﻿
using JinYuan.CommunicationLib.Enum;
using JinYuan.CommunicationLib.Interface;
using JinYuan.DataConvertLib;
using JinYuan.Helper;
using JinYuan.MES.Enums;
using JinYuan.Models;
using JinYuan.VirtualDataLibrary;
using PLCCommunication;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace JinYuan.ControlCenters
{
    public partial class ControlCenter
    {
        private void UpAB(ref BGearEntity m)
        {
            for (int i = 0; i < 10; i++) { }
        }

        #region 保留2位小数
        private float SaveTwoNum(float TNum)
        {
            //1.2333  0.998
            int value = (int)(TNum * 1000) % 10;
            value = value > 4 ? 10 - value : -value;
            TNum += ((float)value / 1000);
            //保留3位小数
            TNum = (float)(int)(TNum * 1000) / 1000;
            return TNum;
        }
        #endregion

        //不良类型
        string NG_Type = "";  // 用于备注
        string OK_NG = "";  // OK或NG
        #region 判断总结果
        /// <summary>
        /// 判断总结果
        /// </summary>
        //private int JudgmentResult(int A, int B, int C, int D, int E, int F, int G, int H, int I, int J, int K, int L, int M, int N)
        //{
        //    //条码，芯包重量，芯包侧厚，芯包CCD极性检测，电芯裁切粘连，电芯裁切毛刺，芯包铜焊印位置，
        //    //芯包正面极耳撕裂H，芯包背面极耳撕裂，芯包正面极耳翻折J，芯包背面极耳翻折,测厚压力结果,
        //    //焊前CCD检测结果,铝焊印位置判定结果

        //    //目前先判断条码，芯包重量，芯包侧厚，芯包CCD极性检测，芯包正面极耳撕裂，芯包正面极耳翻折
        //    int Result = 0;
        //    OK_NG = "OK";
        //    if (A == 2 || B == 2 || C == 2 || D == 2 || H == 2 || J == 2)
        //    {
        //        //ed
        //        Result = 2;
        //        OK_NG = "NG";
        //    }
        //    else if (A == 1 && B == 1 && C == 1 && D == 1 && H == 1 && J == 1)
        //    {
        //        Result = 1;
        //        OK_NG = "OK";
        //    }
        //    else
        //    {
        //        Result = 1;
        //        OK_NG = "OK";
        //    }

        //    if (A == 2)
        //    {
        //        NG_Type += "无条码_";
        //        Result = 2;
        //    }
        //    if (B == 2)
        //    {
        //        NG_Type += "芯包重量1NG_";
        //        //return 2;
        //        Result = 2;
        //    }
        //    if (C == 2)
        //    {
        //        NG_Type += "芯包侧厚NG_";
        //        //return 2;
        //        Result = 2;
        //    }
        //    if (D == 2)
        //    {
        //        NG_Type += "芯包CCD极性检测NG_";
        //        //return 2;
        //        Result = 2;
        //    }

        //    //if (E == 2)
        //    //{
        //    //    NG_Type += "电芯裁切粘连NG_";
        //    //    //return 2;
        //    //}
        //    //if (F == 2)
        //    //{ 
        //    //    NG_Type += "电芯裁切毛刺NG_";
        //    //    //return 2;
        //    //}
        //    //if (G == 2)
        //    //{
        //    //    NG_Type += "芯包铜焊印位置判定NG_";
        //    //    //return 2;
        //    //}
        //    if (H == 2)
        //    {
        //        NG_Type += "芯包正面极耳撕裂NG_";
        //        //return 2;
        //    }
        //    //if (I == 2)
        //    //{
        //    //    NG_Type += "芯包背面极耳撕裂NG_";
        //    //    //return 2;
        //    //}
        //    if (J == 2)
        //    {
        //        NG_Type += "芯包正面极耳翻折NG_";
        //        //return 2;
        //    }
        //    //if (K == 2)
        //    //{
        //    //    NG_Type += "芯包背面极耳翻折NG_";
        //    //    //return 2;
        //    //}
        //    if (L == 2)
        //    {
        //        NG_Type += "测厚压力NG_";
        //        //return 2;
        //    }
        //    if (M == 2)
        //    {
        //        NG_Type += "焊前CCD检测NG_";
        //        //return 2;
        //    }
        //    if (N == 2)
        //    {
        //        NG_Type += "铝焊印位置判定NG_";
        //        //return 2;
        //    }
        //    if (Result == 1)
        //    {
        //        NG_Type += "OK_";
        //    }
        //    return Result;
        //}
        /// <summary>
        /// 判断总结果
        /// </summary>
        private int JudgmentResult(int A, int B, int C, int D, int E, int F, int G, int H, int I, int J, int K, int L, int M, int N)
        {
            //条码，芯包重量，芯包侧厚，芯包CCD极性检测，电芯裁切粘连，电芯裁切毛刺，芯包铜焊印位置，
            //芯包正面极耳撕裂H，芯包背面极耳撕裂，芯包正面极耳翻折J，芯包背面极耳翻折,测厚压力结果,
            //焊前CCD检测结果,铝焊印位置判定结果

            //目前先判断条码，芯包重量，芯包侧厚，芯包CCD极性检测，芯包正面极耳撕裂，芯包正面极耳翻折
            int Result = 0;
            OK_NG = "OK";
            if (A == 2 || B == 2 || C == 2 || D == 2 || H == 2 || J == 2)
            {
                //ed
                Result = 2;
                OK_NG = "NG";
            }
            else if (A == 1 && B == 1 && C == 1 && D == 1 && H == 1 && J == 1)
            {
                Result = 1;
                OK_NG = "OK";
            }
            else
            {
                Result = 1;
                OK_NG = "OK";
            }

            if (A == 2)
            {
                NG_Type += "无条码_";
                Result = 2;
            }
            if (B == 2)
            {
                NG_Type += "芯包重量1NG_";
                //return 2;
                Result = 2;
            }
            if (C == 2)
            {
                NG_Type += "芯包侧厚NG_";
                //return 2;
                Result = 2;
            }
            if (D == 2)
            {
                NG_Type += "芯包CCD极性检测NG_";
                //return 2;
                Result = 2;
            }

            //if (E == 2)
            //{
            //    NG_Type += "电芯裁切粘连NG_";
            //    //return 2;
            //}
            //if (F == 2)
            //{ 
            //    NG_Type += "电芯裁切毛刺NG_";
            //    //return 2;
            //}
            //if (G == 2)
            //{
            //    NG_Type += "芯包铜焊印位置判定NG_";
            //    //return 2;
            //}
            if (H == 2)
            {
                NG_Type += "芯包正面极耳撕裂NG_";
                //return 2;
            }
            //if (I == 2)
            //{
            //    NG_Type += "芯包背面极耳撕裂NG_";
            //    //return 2;
            //}
            if (J == 2)
            {
                NG_Type += "芯包正面极耳翻折NG_";
                //return 2;
            }
            //if (K == 2)
            //{
            //    NG_Type += "芯包背面极耳翻折NG_";
            //    //return 2;
            //}
            if (L == 2)
            {
                NG_Type += "测厚压力NG_";
                //return 2;
            }
            if (M == 2)
            {
                NG_Type += "焊前CCD检测NG_";
                //return 2;
            }
            if (N == 2)
            {
                NG_Type += "铝焊印位置判定NG_";
                //return 2;
            }
            if (Result == 1)
            {
                NG_Type += "OK_";
            }
            return Result;
        }
        private int JudgmentCCDResult(int A, int[,] B, int C)
        {
            var ngType = new string[16] {"正面(2D/3D)结果",
                                        "反面(2D/3D)结果",
                                        "左侧面(2D/3D)结果",
                                        "右侧面(2D/3D)结果",
                                        "顶面(2D/3D)结果",
                                        "底面(2D/3D)结果",
                                        "底DE1结果",
                                        "底DE2结果",
                                        "底DE3结果",
                                        "底DE4结果",
                                        "中ME1结果",
                                        "中ME2结果",
                                        "中ME3结果",
                                        "中ME4结果",
                                        "极柱(POS/NEG)结果",
                                        "防爆阀(PRO)结果"
                                       };
            int Result = 1;
            OK_NG = "OK";           
            for (int i = 0; i < 16; i++)
            {
                if (B[C,i] !=1)
                {
                    //1OK，2扫码NG，3MES反馈NG，4其他NG
                    Result = 4;
                    OK_NG = "NG";
                    NG_Type += $"{ngType[i]}(NG),";
                }
            }
            if (A == 2)
            {
                NG_Type += "无条码,";
                Result = 2;
                OK_NG = "NG";
            }

            return Result;
        }
        private int JudgmentThickNessResult(int[,] B, int C,int ccdResult =1)
        {
            var ngType = new string[3] {"厚度1结果",
                                        "厚度2结果",
                                        "厚度3结果"
                                       };
            int Result = ccdResult;          
            for (int i = 0; i < 3; i++)
            {
                if (B[C, i] != 1)
                {
                    //1OK，2扫码NG，3MES反馈NG，4其他NG
                    Result = 4;
                    OK_NG = "NG";
                    NG_Type += $"{ngType[i]}(NG),";
                }
            }            

            return Result;
        }
        #region 上料工位(数据处理)
        /// <summary>
        /// 上料工位
        /// </summary>
        int JXCount = 0;

        Stopwatch sw2 = new Stopwatch();
        //上料解析
        private void FeedingStation(JYResult<byte[]> bArrays, object plcModel)
        {
            Group pf = plcModel as Group;
            if (bArrays == null)
            {
                CommonMethods.PlcLink.WriteInt16(pf.VarList[0].VarAddress, 0);//置位0
                CommonMethods.AddLog(false, $"电芯进站PLC有触发信号，但无数据!");//存系统日志
                LoggerHelp.WriteLog($"电芯进站PLC有触发信号，但无数据!", "Info");
            }
            if (bArrays.IsSuccess && bArrays.Content != null)
            {


                sw2.Restart();
                //解析数据 
                //1257  3个条码
                List<AGearEntity> list = new List<AGearEntity>();//实体
                List<string> listBarCode = GetList<string>(6, "");//电芯条码
                //List<string> listNum = GetList<string>(1, "");//进站磁悬浮动子编号
                List<short> listRes = GetList<short>(6, 0);//进站电芯反馈结果

                byte[] bytes = bArrays.Content.ByteReverse();//高低位转换

                // 从D2200开始
                #region 数据解析

                //电芯条码
                //从bytes读取第二位参数到第三位参数的内容 0-19 第四位参数为格式 第五位为规则
                //listBarCode[0] = StringLib.GetStringFromByteArrayByEncoding(bArrays.Content, 200, 40, System.Text.Encoding.ASCII).Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
                //listBarCode[1] = StringLib.GetStringFromByteArrayByEncoding(bArrays.Content, 240, 40, System.Text.Encoding.ASCII).Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
                //listBarCode[2] = StringLib.GetStringFromByteArrayByEncoding(bArrays.Content, 280, 40, System.Text.Encoding.ASCII).Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
                //listBarCode[3] = StringLib.GetStringFromByteArrayByEncoding(bArrays.Content, 320, 40, System.Text.Encoding.ASCII).Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
                listBarCode[0] = StringLib.GetStringFromByteArrayByEncoding(bytes, 0, 40, System.Text.Encoding.ASCII).Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
                listBarCode[1] = StringLib.GetStringFromByteArrayByEncoding(bytes, 40, 40, System.Text.Encoding.ASCII).Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
                listBarCode[2] = StringLib.GetStringFromByteArrayByEncoding(bytes, 80, 40, System.Text.Encoding.ASCII).Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
                listBarCode[3] = StringLib.GetStringFromByteArrayByEncoding(bytes, 120, 40, System.Text.Encoding.ASCII).Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
                listBarCode[4] = StringLib.GetStringFromByteArrayByEncoding(bytes, 160, 40, System.Text.Encoding.ASCII).Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
                listBarCode[5] = StringLib.GetStringFromByteArrayByEncoding(bytes, 200, 40, System.Text.Encoding.ASCII).Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
                //listBarCode[0] = "TEST";
                //listBarCode[1] = "TEST";
                //listBarCode[2] = "TEST";
                //listBarCode[3] = "TEST";

                for (int i = 0; i < 6; i++)
                {
                    //有无条码标志
                    int CodeFlag = 1;
                    //极限件标志
                    int ChallengeFlag = 0;
                    AGearEntity m = new AGearEntity();
                    try
                    {
                        //判断条码是否为空
                        if (listBarCode[i] == null || listBarCode[i] == "" || listBarCode[i] == "ERROR")
                        {
                            CodeFlag = 0;
                            //continue;
                        }
                        m.TestTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        m.BarCode = listBarCode[i];

                        //判断是否极限件  查询极限件数据
                        var EX = CommonMethods.db.QuerySqlList<Challenge>(CommonMethods.dBSQL.GetChallengeCode(listBarCode[i]));
                        for (int j = 0; j < EX.Count; j++)
                        {
                            if (string.Compare(EX[j].ChallengeBarCode, listBarCode[i]) == 0)
                            {
                                m.Remark += "(极限件," + EX[j].CodeMessage + "," + EX[j].WeldingCCD + ")";
                                ChallengeFlag = 1;
                            }
                        }

                        if (CodeFlag == 1)
                        {
                            List<string> MesMessage = new List<string>();
                            List<MesResType> mesResType = new List<MesResType>();
                            if (CommonMethods.mesConfig.isUpMes)
                            {
                                string url = CommonMethods.mesConfig.InPutStationMesUrl;
                                string siteCode = CommonMethods.mesConfig.siteCode;
                                string lineCode = CommonMethods.mesConfig.lineCode;
                                string equipNum = CommonMethods.mesConfig.equipNum;
                                string materialCode = CommonMethods.mesConfig.MaterialCode;
                                string userName = CommonMethods.mesConfig.mesUserName;
                                string MesMessage1 = "";
                                MesResType mesResType1 = MesResType.A;

                                bool Res = CommonMethods.hbgMes.SingleCellInStation(url, siteCode, lineCode, equipNum, materialCode, userName, listBarCode[i], ref MesMessage1, ref mesResType1);

                                if (MesMessage1.Contains("发生一个或多个错误"))
                                {
                                    MesMessage1 = "上传失败";
                                }
                                else
                                {
                                    m.Flag = 1;
                                }

                                if (Res)
                                {
                                    m.Result = "OK";
                                    listRes[i] = 1;
                                    m.Remark += MesMessage1;
                                }
                                else
                                {
                                    m.Result = "NG";
                                    switch (mesResType1)
                                    {
                                        case MesResType.A:// 停线，设备报警停机
                                                          //listRes[i] = 10;
                                            listRes[i] = 3;
                                            m.Remark += "MES NG";
                                            break;
                                        case MesResType.B:
                                            //listRes[i] = 9;
                                            listRes[i] = 3;
                                            m.Remark += "MES NG";
                                            break;
                                        case MesResType.C:
                                            //listRes[i] = 1;
                                            listRes[i] = 3;
                                            m.Remark += "MES NG";
                                            break;
                                        case MesResType.D:
                                            listRes[i] = 1;
                                            m.Remark += "MES OK";
                                            break;
                                        default:
                                            listRes[i] = 3;
                                            m.Remark += "MES NG";
                                            break;
                                    }
                                    if (CommonMethods.sysConfig.OKSwitching)
                                    {
                                        //调试发OK
                                        listRes[i] = 1;
                                        //m.Result = "OK";
                                    }
                                    m.Remark += MesMessage1;
                                }
                            }
                            else
                            {
                                m.Flag = 0;
                                m.ID = i + 1;
                                m.CNum = m.ID.ToString();
                                //电芯反馈结果 
                                listRes[i] = 1;
                                m.Result = "OK";
                                m.Remark += "本地存储";
                                m.UpState = "未上传";

                                // m.Remark = "本地存储";

                                //if (CommonMethods.sysConfig.OKSwitching)
                                //{
                                //    //调试发OK
                                //    listRes[j] = 1;
                                //    m.Result = "OK";
                                //}
                                //list.Add(m);
                            }
                        }
                        else
                        {
                            //无条码不上传
                            m.Flag = 0;
                            m.Result = "NG";
                            listRes[i] = 2;
                            m.Remark += "无条码";
                        }

                        //极限件发3给PLC
                        //需要沟通极限  发多少给plc
                        if (ChallengeFlag == 1)
                        {
                            if (string.Compare(EX[0].WeldingCCD, "焊前CCD") == 0)
                            {
                                listRes[i] = 3;
                            }
                            if (string.Compare(EX[0].WeldingCCD, "焊后CCD") == 0)
                            {
                                listRes[i] = 4;
                            }
                        }

                        if (m.Flag == 1)
                            m.UpState = "已上传";
                        else
                            m.UpState = "未上传";

                        list.Add(m);
                    }
                    catch (Exception ex)
                    {
                        LoggerHelp.WriteLog($"外观检测进站线程数据解析：{ex}", "SysErrorLog");
                    }


                }
                #endregion


                //if (CommonMethods.sysConfig.OKSwitching)
                //{
                //    //调试发OK
                //    listRes[0] = 1;
                //    listRes[1] = 1;
                //    listRes[2] = 1;
                //    listRes[3] = 1;
                //    //m.Result = "OK";
                //}
                JYResult result = CommonMethods.PlcLink.WriteValue("D2100", listRes[0], Data_Type.Short);//调试结果写入PLC
                CommonMethods.PlcLink.WriteValue("D2101", listRes[1], Data_Type.Short);//调试结果写入PLC
                CommonMethods.PlcLink.WriteValue("D2102", listRes[2], Data_Type.Short);//调试结果写入PLC
                //CommonMethods.PlcLink.WriteValue("D2103", listRes[3], Data_Type.Short);//调试结果写入PLC
                //CommonMethods.PlcLink.WriteInt16("D2100", listRes[0]);//调试结果OK写入PLC
                if (result != null)
                {
                    string strres = $"分档工位发送给PLC:  D2100={listRes[0]},D2101={listRes[1]},D2102={listRes[2]},";
                    CommonMethods.AddDataMonitorLog(0, strres);
                }

                //写入·结果反馈
                //JYResult result = CommonMethods.PlcLink.WriteValue("D2100", listRes.ToArray(), Data_Type.ArrShort);//判断结果写入PLC
                //JYResult result = CommonMethods.PlcLink.WriteValue("D2100", listRes.ToArray(), Data_Type.ArrShort);//调试结果OK写入PLC

                Task.Run(() => { SendDateMQTT.Instance.EnterandExitSendMessge(0); });
                //读取结果显示
                //数据存储 -异步处理
                Task.Run(() => { SaveFeedingData(list); });
                CommonMethods.PlcLink.WriteInt16(pf.VarList[0].VarAddress, 0);//置位0

                sw2.Stop();
                ////输出上料数据处理耗时：
                CommonMethods.AddDataMonitorLog(0, $"外观检测进站共耗时：{sw2.Elapsed.TotalMilliseconds}ms");

            }
        }



        #region 保存上料数据
        /// <summary>
        /// 保存上料数据到数据
        /// </summary>
        /// <param name="list"></param>
        private void SaveFeedingData(List<AGearEntity> list)
        {
            //数据的保存
            try
            {
                if (list != null && list.Count > 0)
                {
                    //数据显示
                    List<AGearEntity> Blist = list;
                    int i = 0;
                    string strRes = "";
                    foreach (AGearEntity m in list)
                    {
                        if (true)
                        {
                            //int b1 = CommonMethods.dbServer.Delete<DataTurnover>(it => it.containerCode == m.DiRFID);
                            Thread.Sleep(10);
                            //int b2 = CommonMethods.dbServer.Delete<DataTurnover>(it => it.containerCode == m.GaoRFID);

                            //int b1 = CommonMethods.dbServer.Delete<DataTurnover>(it => it.containerCode == m.BarCode);

                            bool b = CommonMethods.db.AddReturnBool<AGearEntity>(m);

                            //strRes = b1 > 0 ? ",远删Yes" : ",远删No";
                            strRes += b;

                            DataTurnover server = new DataTurnover
                            {
                                equipNum = CommonMethods.mesConfig.equipNum,
                                containerCode = m.BarCode,
                                identification = m.BarCode,
                                message = m.Result,
                                Time = DateTime.Now,
                            };

                            try
                            {
                                //bool res = CommonMethods.dbServer.AddReturnBool<DataTurnover>(server);
                                //strRes += res == true ? ",远存Yes" : ",远存No";
                            }
                            catch (Exception ex)
                            {
                                JYResult result = CommonMethods.PlcLink.WriteValue("D80", 1, Data_Type.Short);//反馈PLC结果
                                CommonMethods.AddDataMonitorLog(1, $"服务器连接失败!解决办法：1、请检查MES网络，2、查看服务器是否正常！!");
                                LoggerHelp.WriteLog($"服务器连接失败：{ex}", "系统报错");
                            }
                        }
                        else
                        {
                            strRes = ",不存储";
                        }
                        //m.Remark = m.Remark + strRes;
                        Blist[i].Remark = m.Remark;
                        i++;
                    }
                    //数据显示
                    CommonMethods.ShowFeedingDelegate.Invoke(Blist);
                }
                else
                {
                    LoggerHelp.WriteLog($"电芯进站数据List为空", "SysErrorLog");
                }

            }
            catch (Exception ex)
            {
                LoggerHelp.WriteLog($"外观检测上料保存处理异常：{ex}", "SysErrorLog");
            }
        }
        #endregion

        #endregion

        //NG出站解析
        private void NGStation(JYResult<byte[]> bArrays, object plcModel)
        {
            Group pf = plcModel as Group;
            if (bArrays == null)
            {
                CommonMethods.PlcLink.WriteInt16(pf.VarList[0].VarAddress, 0);//置位0
                CommonMethods.AddLog(false, $"NG出站PLC有触发信号，但无数据!");
                LoggerHelp.WriteLog($"NG出站PLC有触发信号，但无数据!", "SysErrorLog");
            }
            #region 预焊机
            //if (bArrays.IsSuccess && bArrays.Content != null)
            //{
            //    Random random = new Random();
            //    //解析数据  
            //    sw2.Restart();
            //    #region 数据变量定义
            //    List<BGearEntity> list = new List<BGearEntity>();//实体
            //    int[] MyRes = new int[2];//AB反馈结果

            //    #endregion
            //    //高低位转换
            //    byte[] bytes = bArrays.Content.ByteReverse();
            //    byte[] bytes1 = bArrays.Content;
            //    //反馈结果
            //    #region 电芯解析
            //    //D3100起
            //    //电芯解析
            //    #region 上传MES

            //    for (int i = 0; i < 2; i++)
            //    {
            //        try
            //        {
            //            int ChallengeFlag = 0;
            //            BGearEntity m = new BGearEntity();
            //            AGearEntity TT = new AGearEntity();
            //            if (i == 0)
            //            {
            //                m.A_B = "A芯包";
            //                //A_NG芯包信息1  D2442~D2446
            //                for (int j = 0; j < 5; j++)
            //                {
            //                    m.NG_Data1[i, j] = ShortLib.GetShortFromByteArray(bArrays.Content, 84 + j * 2, DataFormat.CDAB);
            //                }

            //                //A_NG芯包信息2  D2452~D2468
            //                for (int j = 0; j < 9; j++)
            //                {
            //                    m.NG_Data2[i, j] = FloatLib.GetFloatFromByteArray(bArrays.Content, 104 + j * 4, DataFormat.CDAB);
            //                }
            //            }
            //            else
            //            {
            //                m.A_B = "B芯包";
            //                //B_NG芯包信息1  D2447~D2451
            //                for (int j = 0; j < 5; j++)
            //                {
            //                    m.NG_Data1[i, j] = ShortLib.GetShortFromByteArray(bArrays.Content, 94 + j * 2, DataFormat.CDAB);
            //                }

            //                //B_NG芯包信息2  D2470~D2486
            //                for (int j = 0; j < 9; j++)
            //                {
            //                    m.NG_Data2[i, j] = FloatLib.GetFloatFromByteArray(bArrays.Content, 140 + j * 4, DataFormat.CDAB);
            //                }
            //            }

            //            //芯包条码 D2400~D3239   
            //            m.CSBYHCODEA = StringLib.GetStringFromByteArrayByEncoding(bArrays.Content, 40 * i, 40, System.Text.Encoding.ASCII).Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
            //            //m.CSBYHCODEA = "TEST";
            //            //判断是否极限件  查询极限件数据
            //            var EX = CommonMethods.db.QuerySqlList<Challenge>(CommonMethods.dBSQL.GetChallengeCode(m.CSBYHCODEA));
            //            for (int j = 0; j < EX.Count; j++)
            //            {
            //                if (string.Compare(EX[j].ChallengeBarCode, m.CSBYHCODEA) == 0)
            //                {
            //                    m.Remark += "(极限件," + EX[j].CodeMessage + "," + EX[j].WeldingCCD + ")";
            //                    ChallengeFlag = 1;
            //                }
            //            }

            //            //查询进站时间数据
            //            var w = CommonMethods.db.QuerySqlList<AGearEntity>(CommonMethods.dBSQL.GetInTime(m.CSBYHCODEA));
            //            //获取最新一行数据
            //            if (w.Count > 0)
            //                TT = w[w.Count - 1];
            //            if (TT.TestTime != null)
            //                m.ScanTime[0] = TT.TestTime;
            //            else
            //                m.ScanTime[0] = "";
            //            //芯包出站时间
            //            m.ScanTime[1] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //            int Code_NG = 0;//条码有无标志 1为有，2为无
            //            //判断有无条码
            //            if (m.CSBYHCODEA != null && m.CSBYHCODEA != "")
            //                Code_NG = 1;
            //            else
            //                Code_NG = 2;

            //            //m.TestTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            //            //不良代码判定
            //            List<string> NGcodeList = new List<string>();
            //            NGcodeList = NGCarcode(m.NG_Data1[i, 1], m.NG_Data1[i, 3], m.NG_Data1[i, 4]);

            //            //芯包重量1判定结果
            //            if (m.NG_Data1[i, 1] == 1)
            //                m.CSBYHWEIGHTRESULTA1 = "OK";
            //            else if (m.NG_Data1[i, 1] == 2)
            //                m.CSBYHWEIGHTRESULTA1 = "NG";
            //            else
            //                m.CSBYHWEIGHTRESULTA1 = "无";
            //            //芯包侧厚判定结果
            //            if (m.NG_Data1[i, 3] == 1)
            //                m.CSBYHTHICKNESSTESTJUDGERESULTA = "OK";
            //            else if (m.NG_Data1[i, 3] == 2)
            //                m.CSBYHTHICKNESSTESTJUDGERESULTA = "NG";
            //            else
            //                m.CSBYHTHICKNESSTESTJUDGERESULTA = "无";
            //            //芯包CCD极性检测结果
            //            if (m.NG_Data1[i, 4] == 1)
            //                m.CSBYHPOLARITYRESULTA = "OK";
            //            else if (m.NG_Data1[i, 4] == 2)
            //                m.CSBYHPOLARITYRESULTA = "NG";
            //            else
            //                m.CSBYHPOLARITYRESULTA = "无";
            //            //电芯裁切粘连判定结果
            //            m.CSBYHCUTADHESIONJUDGERESULTA = "无";
            //            //电芯裁切毛刺判定结果
            //            m.CSBYHCUTBURRJUDGERESULTA = "无";
            //            //芯包焊印位置判定结果
            //            m.CSBYHSOLDERJUDGERESULTA = "无";
            //            m.CSBYHSOLDERJUDGERESULTA2 = "无";
            //            m.AluminiumWeldPositionResult1 = "无";
            //            m.AluminiumWeldPositionResult2 = "无";
            //            //芯包正面极耳撕裂判定结果
            //            m.CSBYHFRONTEARCRACKLEJUDGERESULTA = "无";
            //            //芯包背面极耳撕裂判定结果
            //            m.CSBYHBACKEARCRACKLEJUDGERESULTA = "无";
            //            //芯包正面极耳翻折检测结果
            //            m.CSBYHFRONTEARFLODINGRESULTA = "无";
            //            //芯包背面极耳翻折检测结果
            //            m.CSBYHBACKEARFLODINGRESULTA = "无";


            //            m.CSBYHASCANNINGTIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //            m.CSBYHWEIGHTNOA1 = m.NG_Data1[i, 2];
            //            m.CSBYHTHICKNESSTESTSTATIONNOA = m.NG_Data1[i, 4];
            //            m.CSBYHTOOLNOA = 0;
            //            m.CSBYHCUWELDINGSTATIONNOA1 = 0;
            //            m.CSBYHCUWELDINGSTATIONNOA2 = 0;
            //            m.CSBYHCUWELDINGSTATIONNOA3 = 0;
            //            m.CSBYHCUWELDHEADCOUNTA1 = 0;
            //            m.CSBYHCUWELDHEADCOUNTA2 = 0;
            //            m.CSBYHCUWELDHEADCOUNTA3 = 0;
            //            m.CSBYHCUWELDBASECOUNTA1 = 0;
            //            m.CSBYHCUWELDBASECOUNTA2 = 0;
            //            m.CSBYHCUWELDBASECOUNTA3 = 0;
            //            m.CSBYHALWELDINGSTATIONNOA1 = 0;
            //            m.CSBYHALWELDINGSTATIONNOA2 = 0;
            //            m.CSBYHALWELDINGSTATIONNOA3 = 0;
            //            m.CSBYHALWELDHEADCOUNTA1 = 0;
            //            m.CSBYHALWELDHEADCOUNTA2 = 0;
            //            m.CSBYHALWELDHEADCOUNTA3 = 0;
            //            m.CSBYHALWELDBASECOUNTA1 = 0;
            //            m.CSBYHALWELDBASECOUNTA2 = 0;
            //            m.CSBYHALWELDBASECOUNTA3 = 0;
            //            m.CSBYHCUTABCUTTERCOUNTA = 0;
            //            m.CSBYHALTABCUTTERCOUNTA = 0;

            //            m.CSBYHCUTABCUTTERNOA = 0;
            //            m.CSBYHALTABCUTTERNOA = 0;
            //            m.CSBYHPROCESSNAME = "超声波预焊接";
            //            m.CSBYHSCHEDULE = CommonMethods.strClass;
            //            m.CSBYHINSTATIONTIME = m.ScanTime[0];
            //            m.CSBYHOUTSTATIONTIME = m.ScanTime[1];
            //            //m.CSBYHABNORMALINFORMATION = m.list_Process[i, 4];
            //            m.CSBYHWEIGHTA1MAXSETTING = SaveTwoNum(m.NG_Data2[i, 0]);
            //            m.CSBYHWEIGHTA1MINSETTING = SaveTwoNum(m.NG_Data2[i, 1]);
            //            m.CSBYHWEIGHTA1 = SaveTwoNum(m.NG_Data2[i, 2]);
            //            m.CSBYHTHICKNESSTESTPRESSUREMAXSETTINGA = SaveTwoNum(m.NG_Data2[i, 3]);
            //            m.CSBYHTHICKNESSTESTPRESSUREMINSETTINGA = SaveTwoNum(m.NG_Data2[i, 4]);
            //            m.CSBYHTHICKNESSTESTPRESSUREA = SaveTwoNum(m.NG_Data2[i, 5]);
            //            m.CSBYHTHICKNESSTESTPRESSURESIZEA = SaveTwoNum(m.NG_Data2[i, 6]);
            //            m.CSBYHTHICKNESSTESTPRESSUREMAXSIZEA = SaveTwoNum(m.NG_Data2[i, 7]);
            //            m.CSBYHTHICKNESSTESTPRESSUREMINSIZEA = SaveTwoNum(m.NG_Data2[i, 8]);
            //            m.CSBYHCUWELDINGPRESSUREMAXSETTING1 = 0;
            //            m.CSBYHCUWELDINGPRESSUREMAXSETTING2 = 0;
            //            m.CSBYHCUWELDINGPRESSUREMAXSETTING3 = 0;
            //            m.CSBYHCUWELDINGPRESSUREMINSETTING = 0;//压力最小值
            //            m.CSBYHCUWELDINGPOWERMAXSETTING1 = 0;
            //            m.CSBYHCUWELDINGPOWERMAXSETTING2 = 0;
            //            m.CSBYHCUWELDINGPOWERMAXSETTING3 = 0;
            //            m.CSBYHCUWELDINGPOWERMINSETTING1 = 0;
            //            m.CSBYHCUWELDINGPOWERMINSETTING2 = 0;
            //            m.CSBYHCUWELDINGPOWERMINSETTING3 = 0;
            //            m.CSBYHCUWELDINGENERGYMAXSETTING1 = 0;
            //            m.CSBYHCUWELDINGENERGYMAXSETTING2 = 0;
            //            m.CSBYHCUWELDINGENERGYMAXSETTING3 = 0;
            //            m.CSBYHCUWELDINGENERGYMINSETTING1 = 0;
            //            m.CSBYHCUWELDINGENERGYMINSETTING2 = 0;
            //            m.CSBYHCUWELDINGENERGYMINSETTING3 = 0;
            //            m.CSBYHCUWELDINGAMPLITUDEMAXSETTING1 = 0;
            //            m.CSBYHCUWELDINGAMPLITUDEMAXSETTING2 = 0;
            //            m.CSBYHCUWELDINGAMPLITUDEMAXSETTING3 = 0;
            //            m.CSBYHCUWELDINGAMPLITUDEMINSETTING1 = 0;
            //            m.CSBYHCUWELDINGAMPLITUDEMINSETTING2 = 0;
            //            m.CSBYHCUWELDINGAMPLITUDEMINSETTING3 = 0;
            //            m.CSBYHCUPRESSUREA1 = 0;
            //            m.CSBYHCUPOWERA1 = 0;
            //            m.CSBYHCUENERGYA1 = 0;
            //            m.CSBYHCUAMPLITUDEA1 = 0;
            //            m.CSBYHCUTIMEA1 = 0;
            //            m.CSBYHCUPRESSUREA2 = 0;
            //            m.CSBYHCUPOWERA2 = 0;
            //            m.CSBYHCUENERGYA2 = 0;
            //            m.CSBYHCUAMPLITUDEA2 = 0;
            //            m.CSBYHCUTIMEA2 = 0;
            //            m.CSBYHCUPRESSUREA3 = 0;
            //            m.CSBYHCUPOWERA3 = 0;
            //            m.CSBYHCUENERGYA3 = 0;
            //            m.CSBYHCUAMPLITUDEA3 = 0;
            //            m.CSBYHCUTIMEA3 = 0;
            //            m.CSBYHALWELDINGPRESSUREMAXSETTING1 = 0;
            //            m.CSBYHALWELDINGPRESSUREMAXSETTING2 = 0;
            //            m.CSBYHALWELDINGPRESSUREMAXSETTING3 = 0;
            //            m.CSBYHALWELDINGPRESSUREMINSETTING = 0;
            //            m.CSBYHALWELDINGPOWERMAXSETTING1 = 0;
            //            m.CSBYHALWELDINGPOWERMAXSETTING2 = 0;
            //            m.CSBYHALWELDINGPOWERMAXSETTING3 = 0;
            //            m.CSBYHALWELDINGPOWERMINSETTING1 = 0;
            //            m.CSBYHALWELDINGPOWERMINSETTING2 = 0;
            //            m.CSBYHALWELDINGPOWERMINSETTING3 = 0;
            //            m.CSBYHALWELDINGENERGYMAXSETTING1 = 0;
            //            m.CSBYHALWELDINGENERGYMAXSETTING2 = 0;
            //            m.CSBYHALWELDINGENERGYMAXSETTING3 = 0;
            //            m.CSBYHALWELDINGENERGYMINSETTING1 = 0;
            //            m.CSBYHALWELDINGENERGYMINSETTING2 = 0;
            //            m.CSBYHALWELDINGENERGYMINSETTING3 = 0;
            //            m.CSBYHALWELDINGAMPLITUDEMAXSETTING1 = 0;
            //            m.CSBYHALWELDINGAMPLITUDEMAXSETTING2 = 0;
            //            m.CSBYHALWELDINGAMPLITUDEMAXSETTING3 = 0;
            //            m.CSBYHALWELDINGAMPLITUDEMINSETTING1 = 0;
            //            m.CSBYHALWELDINGAMPLITUDEMINSETTING2 = 0;
            //            m.CSBYHALWELDINGAMPLITUDEMINSETTING3 = 0;
            //            m.CSBYHALPRESSUREA1 = 0;
            //            m.CSBYHALPOWERA1 = 0;
            //            m.CSBYHALENERGYA1 = 0;
            //            m.CSBYHALAMPLITUDEA1 = 0;
            //            m.CSBYHALTIMEA1 = 0;
            //            m.CSBYHALPRESSUREA2 = 0;
            //            m.CSBYHALPOWERA2 = 0;
            //            m.CSBYHALENERGYA2 = 0;
            //            m.CSBYHALAMPLITUDEA2 = 0;
            //            m.CSBYHALTIMEA2 = 0;
            //            m.CSBYHALPRESSUREA3 = 0;
            //            m.CSBYHALPOWERA3 = 0;
            //            m.CSBYHALENERGYA3 = 0;
            //            m.CSBYHALAMPLITUDEA3 = 0;
            //            m.CSBYHALTIMEA3 = 0;
            //            m.CSBYHCUTABLENGTHA = 0;
            //            m.CSBYHCUTABWIDTHA = 0;
            //            m.CSBYHALTABLENGTHA = 0;
            //            m.CSBYHALTABWIDTHA = 0;
            //            m.CSBYHTOPTOSOLDERDISTANCEA = 0;
            //            m.CSBYHTOPTOSOLDERDISTANCEA2 = 0;
            //            m.AluminumToWeld1 = 0;
            //            m.AluminumToWeld2 = 0;
            //            m.CSBYHSOLDERTOEARDISTANCEA = 0;
            //            //MyRes[i] = JudgmentResult(Code_NG, m.list_Data1[i, 1], m.list_Data1[i, 4], m.list_Data1[i, 5], m.list_Data1[i, 15],
            //            //    m.list_Data1[i, 16], m.list_Data1[i, 17], m.list_Data1[i, 18], m.list_Data1[i, 19],
            //            //    m.list_Data1[i, 20], m.list_Data1[i, 21], m.list_Data2[i, 59], m.list_Data2[i, 60],
            //            //    m.list_Data2[i, 63], m.list_Data2[i, 64]);

            //            //初值OK
            //            m.Result = m.listRes[i] = 1;
            //            m.CSBYHOUTSTATIONJUDGMENTRESULT = "OK";

            //            //优先级 ：条码  MES  极性 重量 测厚压力 测厚位移 （焊前CCD 正面极耳撕裂和翻折）
            //            //无条码则无后面检测

            //            if (m.NG_Data1[i, 1] == 2 || m.NG_Data1[i, 3] == 2 || m.NG_Data1[i, 4] == 2)
            //            {
            //                m.Result = m.listRes[i] = 2;
            //                m.CSBYHOUTSTATIONJUDGMENTRESULT = "NG";
            //            }

            //            list.Add(m);

            //            string ur2 = CommonMethods.mesConfig.ResultProcessParamMesUrl;
            //            string siteCode1 = CommonMethods.mesConfig.siteCode;
            //            string lineCode1 = CommonMethods.mesConfig.lineCode;
            //            string equipNum1 = CommonMethods.mesConfig.equipNum;
            //            string materialCode1 = CommonMethods.mesConfig.MaterialCode;
            //            string userName1 = CommonMethods.mesConfig.mesUserName;
            //            MesUploadType unloadType = MesUploadType.DD;
            //            string Mesage = "";
            //            //加判断无条码不上传mes
            //            m.Remark += "NG小车1，";
            //            //芯包测厚（位移）判定结果
            //            if (m.NG_Data1[i, 3] == 2) { m.Remark += "芯包侧厚（位移）NG,"; }
            //            //芯包测厚（压力）判定结果  （）
            //            if (false) { m.Remark += "芯包侧厚（压力）NG,"; }
            //            //芯包重量1判定结果
            //            if (m.NG_Data1[i, 1] == 2) { m.Remark += "芯包重量NG,"; }
            //            //芯包CCD极性检测结果
            //            if (m.NG_Data1[i, 4] == 2) { m.Remark += "芯包CCD极性检测NG,"; }
            //            if (Code_NG == 1)
            //            {
            //                string MesMessage = "";
            //                MesResType mesResType = MesResType.C;

            //                //m.Result = MyRes[i] = m.listRes[i] = 2;
            //                //m.UpState = "未上传";
            //                //m.Remark = "出站前NG";


            //                if (CommonMethods.mesConfig.isUpMes)
            //                {
            //                    string url = CommonMethods.mesConfig.OutStationMesUrl;
            //                    string siteCode = CommonMethods.mesConfig.siteCode;
            //                    string lineCode = CommonMethods.mesConfig.lineCode;
            //                    string equipNum = CommonMethods.mesConfig.equipNum;
            //                    string materialCode = CommonMethods.mesConfig.MaterialCode;
            //                    string userName = CommonMethods.mesConfig.mesUserName;
            //                    int type = 1;
            //                    //int type = 1;
            //                    //string materialLotCodeLocator = "";
            //                    #region NG小车排料调出站
            //                    bool Res = CommonMethods.hbgMes.OutboundInformations(url, siteCode, lineCode, equipNum, materialCode, userName, m, ref MesMessage, ref mesResType, NGcodeList);
            //                    //bool Res = CommonMethods.hbgMes.SingleCellOutStation(url, siteCode, lineCode, equipNum, materialCode, userName, m,)
            //                    if (MesMessage.Contains("发生一个或多个错误"))
            //                    {
            //                        MesMessage = "上传失败,";
            //                    }
            //                    else
            //                    {
            //                        m.Flag = 1;
            //                    }


            //                    //m.UpState = "已上传";
            //                    if (Res)
            //                    {
            //                        m.Remark += MesMessage;
            //                    }
            //                    else
            //                    {
            //                        m.CSBYHOUTSTATIONJUDGMENTRESULT = "NG";
            //                        switch (mesResType)
            //                        {
            //                            case MesResType.A:// 停线，设备报警停机
            //                                m.listRes[i] = 2;
            //                                MyRes[i] = m.listRes[i];
            //                                break;
            //                            case MesResType.B://不停机，MEs NG品要排出，要给PLC发NG信号
            //                                m.listRes[i] = 2;
            //                                MyRes[i] = m.listRes[i];
            //                                break;
            //                            case MesResType.C://可忽略
            //                                m.listRes[i] = 2;
            //                                MyRes[i] = m.listRes[i];
            //                                break;
            //                            default:
            //                                m.listRes[i] = 2;
            //                                MyRes[i] = m.listRes[i];
            //                                break;

            //                        }
            //                        ////芯包测厚（位移）判定结果
            //                        //if (m.NG_Data1[i, 3] == 2) { m.Remark += "芯包侧厚（位移）NG,"; }
            //                        ////芯包测厚（压力）判定结果
            //                        //if (false) { m.Remark += "芯包侧厚（压力）NG,"; }
            //                        ////芯包重量1判定结果
            //                        //if (m.NG_Data1[i, 1] == 2) { m.Remark += "芯包重量NG,"; }
            //                        ////芯包CCD极性检测结果
            //                        //if (m.NG_Data1[i, 4] == 2) { m.Remark += "芯包CCD极性检测NG,"; }
            //                        m.Remark += MesMessage;
            //                    }
            //                    #endregion


            //                    bool Res1 = CommonMethods.hbgMes.ProducResultParam(ur2, siteCode1, lineCode1, equipNum1, materialCode1, userName1, m, unloadType, ref Mesage);

            //                    if (m.Flag == 1)
            //                        m.UpState = "已上传";
            //                    else
            //                        m.UpState = "未上传";


            //                }
            //                else
            //                {
            //                    m.CSBYHOUTSTATIONJUDGMENTRESULT = "NG";
            //                    m.Result = MyRes[i] = m.listRes[i] = 2;
            //                    m.UpState = "未上传";
            //                    ////芯包测厚（位移）判定结果
            //                    //if (m.NG_Data1[i, 3] == 2) { m.Remark += "芯包侧厚（位移）NG,"; }
            //                    ////芯包测厚（压力）判定结果
            //                    //if (false) { m.Remark += "芯包侧厚（压力）NG,"; }
            //                    ////芯包重量1判定结果
            //                    //if (m.NG_Data1[i, 1] == 2) { m.Remark += "芯包重量NG,"; }
            //                    ////芯包CCD极性检测结果
            //                    //if (m.NG_Data1[i, 4] == 2) { m.Remark += "芯包CCD极性检测NG,"; }
            //                }

            //            }
            //            else
            //            {
            //                m.CSBYHOUTSTATIONJUDGMENTRESULT = "NG";
            //                m.Result = MyRes[i] = m.listRes[i] = 2;
            //                m.Remark += "无条码,本地存储";
            //                //正常无条码，以下不会NG不会出现
            //                ////芯包重量1判定结果
            //                //if (m.NG_Data1[i, 1] == 2) { m.Remark += ",芯包重量NG"; }
            //                ////芯包侧厚判定结果
            //                //if (m.NG_Data1[i, 3] == 2) { m.Remark += ",芯包侧厚NG"; }
            //                ////芯包CCD极性检测结果
            //                //if (m.NG_Data1[i, 4] == 2) { m.Remark += ",芯包CCD极性检测NG"; }
            //                m.UpState = "未上传";
            //            }

            //            //极限件发3给PLC
            //            //if (ChallengeFlag == 1)
            //            //{
            //            //    if (string.Compare(EX[0].WeldingCCD, "焊前CCD") == 0)
            //            //    {
            //            //        MyRes[i] = 3;
            //            //    }
            //            //    if (string.Compare(EX[0].WeldingCCD, "焊后CCD") == 0)
            //            //    {
            //            //        MyRes[i] = 4;
            //            //    }
            //            //}

            //            Task.Run(() => { SendDateMQTT.Instance.EnterandExitSendMessge(1); });

            //        }
            //        catch (Exception ex)
            //        {
            //            LogHelper.Instance.WriteError($"外观检测出站线程数据解析：{ex}", "SysErrorLog");
            //        }
            //    }

            //    #endregion
            //    JYResult result1 = null;
            //    JYResult result2 = null;
            //    if (pf.IsFeedbackPlc)
            //    {
            //        //result1 = CommonMethods.PlcLink.WriteValue(pf.VarList[2].VarAddress, MyRes[0], Data_Type.Short);//判断结果写入PLC
            //        //result2 = CommonMethods.PlcLink.WriteValue(pf.VarList[3].VarAddress, MyRes[1], Data_Type.Short);//判断结果写入PLC
            //    }
            //    JYResult result3 = CommonMethods.PlcLink.WriteValue(pf.VarList[0].VarAddress, 0);//置位0
            //    //CommonMethods.AddDataMonitorLog(0, $"发送信息给PLC:{result1.IsSuccess},{MyRes[0]},{MyRes[1]}");
            //    //LogHelper.Instance.WriteLog($"发送信息给PLC:{result1.IsSuccess},{MyRes[0]},{MyRes[1]}");

            //    //数据存储 - 异步处理
            //    Task.Run(() =>
            //    {
            //        SaveBlankingData(list);
            //    });

            //    sw2.Stop();
            //    ////输出下料数据处理耗时：
            //    CommonMethods.AddDataMonitorLog(0, $"NG出站共耗时：{sw2.Elapsed.TotalMilliseconds}ms");
            //}
            #endregion
        }


        #region 下料工位(数据处理)
        //int OutCount = 0;
        #region //下料解析 预焊检
        //private void BlankingStation_YHJ(JYResult<byte[]> bArrays, object plcModel)
        //{
        //    Group pf = plcModel as Group;
        //    if (bArrays == null)
        //    {
        //        CommonMethods.PlcLink.WriteInt16(pf.VarList[0].VarAddress, 0);//置位0
        //        CommonMethods.AddLog(false, $"电芯出站PLC有触发信号，但无数据!");
        //        LogHelper.Instance.WriteLog($"电芯出站PLC有触发信号，但无数据!", "SysErrorLog");
        //    }
        //    if (bArrays.IsSuccess && bArrays.Content != null)
        //    {
        //        Random random = new Random();
        //        //解析数据  
        //        sw2.Restart();
        //        #region 数据变量定义
        //        List<BGearEntity> list = new List<BGearEntity>();//实体
        //        //int[] MyRes = new int[2];//AB反馈结果
        //        int[] MyRes = new int[3];//三个外观检测反馈结果
        //        #endregion
        //        //高低位转换
        //        byte[] bytes = bArrays.Content.ByteReverse();
        //        byte[] bytes1 = bArrays.Content;

        //        List<string> listBarCode = GetList<string>(3, "");//电芯条码
        //        //List<string> listNum = GetList<string>(1, "");//进站磁悬浮动子编号
        //        List<short> listRes = GetList<short>(3, 0);//出站电芯反馈结果
        //        //反馈结果
        //        #region 电芯解析
        //        //电芯解析  从3200 开始              
        //        listBarCode[0] = StringLib.GetStringFromByteArrayByEncoding(bArrays.Content, 0, 40, System.Text.Encoding.ASCII).Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
        //        listBarCode[1] = StringLib.GetStringFromByteArrayByEncoding(bArrays.Content, 40, 40, System.Text.Encoding.ASCII).Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
        //        listBarCode[2] = StringLib.GetStringFromByteArrayByEncoding(bArrays.Content, 80, 40, System.Text.Encoding.ASCII).Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
        //        #region 上传MES
        //        //D3100起
        //        for (int i = 0; i < 3; i++)
        //        {
        //            try
        //            {
        //                BGearEntity m = new BGearEntity();
        //                AGearEntity TT = new AGearEntity();
        //                //极限件标志
        //                int ChallengeFlag = 0;
        //                if (i == 0)
        //                {
        //                    //为了同时上传AB消息而增加,后续可能不需要
        //                    UpAB(ref m);

        //                    m.A_B = "A芯包";
        //                    //芯包信息1  D3300~D3326
        //                    for (int j = 0; j < 27; j++)
        //                    {
        //                        m.list_Data1[i, j] = ShortLib.GetShortFromByteArray(bArrays.Content, 280 + j * 2, DataFormat.CDAB);
        //                    }
        //                    //芯包信息2  D3330~D3356
        //                    for (int j = 0; j < 27; j++)
        //                    {
        //                        m.list_Data2[i, j] = ShortLib.GetShortFromByteArray(bArrays.Content, 280 + j * 2, DataFormat.CDAB);
        //                    }
        //                    //芯包信息3  D3360~D3386
        //                    for (int j = 0; j < 27; j++)
        //                    {
        //                        m.list_Data3[i, j] = ShortLib.GetShortFromByteArray(bArrays.Content, 280 + j * 2, DataFormat.CDAB);
        //                    }






        //                    //A芯包信息1  D3240~D3262
        //                    for (int j = 0; j < 28; j++)
        //                    {
        //                        m.list_Data1[i, j] = ShortLib.GetShortFromByteArray(bArrays.Content, 280 + j * 2, DataFormat.CDAB);
        //                    }
        //                    //A芯包信息2（焊机相关次数）  D3300~D3326
        //                    for (int j = 0; j < 19; j++)
        //                    {
        //                        m.list_Data2[i, j] = IntLib.GetIntFromByteArray(bArrays.Content, 400 + j * 4, DataFormat.CDAB);
        //                    }
        //                    //A芯包信息3(设定值)  D3400~D3506
        //                    for (int j = 0; j < 54; j++)
        //                    {
        //                        m.list_Data3[i, j] = FloatLib.GetFloatFromByteArray(bArrays.Content, 600 + j * 4, DataFormat.CDAB);
        //                    }

        //                    //A芯包信息4(实际值)  D3550~D3634
        //                    for (int j = 0; j < 47; j++)
        //                    {
        //                        m.list_Data4[i, j] = FloatLib.GetFloatFromByteArray(bArrays.Content, 900 + j * 4, DataFormat.CDAB);
        //                    }
        //                    for (int j = 0; j <1; j++)
        //                    {
        //                        m.list_pan[i, j] = ShortLib.GetShortFromByteArray(bArrays.Content,1088 + j * 2, DataFormat.CDAB);
        //                    }

        //                }
        //                else
        //                {
        //                    m.A_B = "B芯包";
        //                    //B芯包信息1  D3740~D3762
        //                    for (int j = 0; j < 28; j++)
        //                    {
        //                        m.list_Data1[i, j] = ShortLib.GetShortFromByteArray(bArrays.Content, 1280 + j * 2, DataFormat.CDAB);
        //                    }
        //                    //B芯包信息2（焊机相关次数）  D3800~D3826
        //                    for (int j = 0; j < 19; j++)
        //                    {
        //                        m.list_Data2[i, j] = IntLib.GetIntFromByteArray(bArrays.Content, 1400 + j * 4, DataFormat.CDAB);
        //                    }
        //                    //B芯包信息3(设定值)  D3900~D4006
        //                    for (int j = 0; j < 54; j++)
        //                    {
        //                        m.list_Data3[i, j] = FloatLib.GetFloatFromByteArray(bArrays.Content, 1600 + j * 4, DataFormat.CDAB);
        //                    }

        //                    //B芯包信息4(实际值)  D4050~D4134
        //                    for (int j = 0; j < 47; j++)
        //                    {
        //                        m.list_Data4[i, j] = FloatLib.GetFloatFromByteArray(bArrays.Content, 1900 + j * 4, DataFormat.CDAB);
        //                    }
        //                    for (int j = 0; j < 1; j++)
        //                    {
        //                        m.list_pan[i, j] = ShortLib.GetShortFromByteArray(bArrays.Content, 1188 + j * 2, DataFormat.CDAB);
        //                    }
        //                }
        //                //芯包扫码时间
        //                //m.ScanTime[i] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        //                //芯包条码 D3200~D3239   
        //                m.CSBYHCODEA = StringLib.GetStringFromByteArrayByEncoding(bytes1, 200 + 40 * i, 40, System.Text.Encoding.ASCII).Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
        //                //m.CSBYHCODEA = "TEST";

        //                var EX = CommonMethods.db.QuerySqlList<Challenge>(CommonMethods.dBSQL.GetChallengeCode(m.CSBYHCODEA));
        //                for (int j = 0; j < EX.Count; j++)
        //                {
        //                    if (string.Compare(EX[j].ChallengeBarCode, m.CSBYHCODEA) == 0)
        //                    {
        //                        m.Remark += "(极限件," + EX[j].CodeMessage + "," + EX[j].WeldingCCD + ")";
        //                        ChallengeFlag = 1;
        //                    }
        //                }
        //                //查询进站时间数据
        //                var w = CommonMethods.db.QuerySqlList<AGearEntity>(CommonMethods.dBSQL.GetInTime(m.CSBYHCODEA));
        //                //获取新一行数据
        //                if (w.Count > 0)
        //                    TT = w[w.Count - 1];
        //                //
        //                //if (w.Count > 0)
        //                //    TT = w[0];
        //                if (TT.TestTime != null)
        //                    m.ScanTime[0] = TT.TestTime;
        //                else
        //                    m.ScanTime[0] = "";
        //                //芯包出站时间
        //                m.ScanTime[1] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

        //                //复投次数
        //                m.RethrowNum = 0;
        //                if (w.Count > 0)
        //                {
        //                    int cccc = w.Count;
        //                    m.RethrowNum = cccc - 1;
        //                }

        //                int Code_NG = 0;//条码有无标志 1为有，2为无
        //                //判断有无条码
        //                if (m.CSBYHCODEA != null && m.CSBYHCODEA != "" && m.CSBYHCODEA != "ERROR")
        //                    Code_NG = 1;
        //                else
        //                    Code_NG = 2;

        //                //m.ChannelNo = m.MyNo[i];
        //                //m.TestTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

        //                //不良代码判定
        //                List<string> NGcodeList = new List<string>();
        //                NGcodeList = NGcode(m.list_Data1[i, 10], m.list_Data1[i, 11], m.list_Data1[i, 12], m.list_Data1[i, 15], m.list_Data1[i, 26], m.list_Data1[i, 16]
        //                    , m.list_Data1[i, 17], m.list_Data1[i, 18], m.list_Data1[i, 19], m.list_Data1[i, 23], m.list_Data1[i, 24], m.list_Data1[i, 25], m.list_Data1[i, 27]);

        //                //芯包重量1判定结果
        //                if (m.list_Data1[i, 10] == 1)
        //                    m.CSBYHWEIGHTRESULTA1 = "OK";
        //                else if (m.list_Data1[i, 10] == 2)
        //                    m.CSBYHWEIGHTRESULTA1 = "NG";
        //                else
        //                    m.CSBYHWEIGHTRESULTA1 = "OK";
        //                //芯包侧厚判定结果
        //                if (m.list_Data1[i, 11] == 1)
        //                    m.CSBYHTHICKNESSTESTJUDGERESULTA = "OK";
        //                else if (m.list_Data1[i, 11] == 2)
        //                    m.CSBYHTHICKNESSTESTJUDGERESULTA = "NG";
        //                else
        //                    m.CSBYHTHICKNESSTESTJUDGERESULTA = "OK";
        //                //芯包CCD极性检测结果
        //                if (m.list_Data1[i, 12] == 1)
        //                    m.CSBYHPOLARITYRESULTA = "OK";
        //                else if (m.list_Data1[i, 12] == 2)
        //                    m.CSBYHPOLARITYRESULTA = "NG";
        //                else
        //                    m.CSBYHPOLARITYRESULTA = "OK";
        //                //电芯裁切粘连判定结果
        //                if (m.list_Data1[i, 13] == 1)
        //                    m.CSBYHCUTADHESIONJUDGERESULTA = "OK";
        //                else if (m.list_Data1[i, 13] == 2)
        //                    m.CSBYHCUTADHESIONJUDGERESULTA = "NG";
        //                else
        //                    m.CSBYHCUTADHESIONJUDGERESULTA = "OK";
        //                //电芯裁切毛刺判定结果
        //                if (m.list_Data1[i, 14] == 1)
        //                    m.CSBYHCUTBURRJUDGERESULTA = "OK";
        //                else if (m.list_Data1[i, 14] == 2)
        //                    m.CSBYHCUTBURRJUDGERESULTA = "NG";
        //                else
        //                    m.CSBYHCUTBURRJUDGERESULTA = "OK";
        //                //芯包铜焊印位置判定结果1
        //                if (m.list_Data1[i, 15] == 1)
        //                {
        //                    m.CSBYHSOLDERJUDGERESULTA = "OK";
        //                }
        //                else if (m.list_Data1[i, 15] == 2)
        //                {
        //                    m.CSBYHSOLDERJUDGERESULTA = "NG";
        //                }
        //                else
        //                {
        //                    m.CSBYHSOLDERJUDGERESULTA = "OK";
        //                }
        //                //芯包铜焊印位置判定结果2
        //                if (m.list_Data1[i, 26] == 1)
        //                {
        //                    m.CSBYHSOLDERJUDGERESULTA2 = "OK";
        //                }
        //                else if (m.list_Data1[i, 26] == 2)
        //                {
        //                    m.CSBYHSOLDERJUDGERESULTA2 = "NG";
        //                }
        //                else
        //                {
        //                    m.CSBYHSOLDERJUDGERESULTA2 = "OK";
        //                }
        //                //芯包正面极耳撕裂判定结果
        //                if (m.list_Data1[i, 16] == 1)
        //                    m.CSBYHFRONTEARCRACKLEJUDGERESULTA = "OK";
        //                else if (m.list_Data1[i, 16] == 2)
        //                    m.CSBYHFRONTEARCRACKLEJUDGERESULTA = "NG";
        //                else
        //                    m.CSBYHFRONTEARCRACKLEJUDGERESULTA = "OK";
        //                //芯包背面极耳撕裂判定结果
        //                if (m.list_Data1[i, 17] == 1)
        //                    m.CSBYHBACKEARCRACKLEJUDGERESULTA = "OK";
        //                else if (m.list_Data1[i, 17] == 2)
        //                    m.CSBYHBACKEARCRACKLEJUDGERESULTA = "NG";
        //                else
        //                    m.CSBYHBACKEARCRACKLEJUDGERESULTA = "OK";
        //                //芯包正面极耳翻折检测结果
        //                if (m.list_Data1[i, 18] == 1)
        //                    m.CSBYHFRONTEARFLODINGRESULTA = "OK";
        //                else if (m.list_Data1[i, 18] == 2)
        //                    m.CSBYHFRONTEARFLODINGRESULTA = "NG";
        //                else
        //                    m.CSBYHFRONTEARFLODINGRESULTA = "OK";
        //                //芯包背面极耳翻折检测结果
        //                if (m.list_Data1[i, 19] == 1)
        //                    m.CSBYHBACKEARFLODINGRESULTA = "OK";
        //                else if (m.list_Data1[i, 19] == 2)
        //                    m.CSBYHBACKEARFLODINGRESULTA = "NG";
        //                else
        //                    m.CSBYHBACKEARFLODINGRESULTA = "OK";

        //                //电芯测厚压力结果
        //                if (m.list_Data1[i, 23] == 1)
        //                    m.ThicknessPressureResult = "OK";
        //                else if (m.list_Data1[i, 23] == 2)
        //                    m.ThicknessPressureResult = "NG";
        //                else
        //                    m.ThicknessPressureResult = "OK";
        //                //芯包焊前CCD检测结果
        //                if (m.list_Data1[i, 24] == 1)
        //                    m.PreweldResult = "OK";
        //                else if (m.list_Data1[i, 24] == 2)
        //                    m.PreweldResult = "NG";
        //                else
        //                    m.PreweldResult = "OK";
        //                //芯包铝焊印位置判定结果1
        //                if (m.list_Data1[i, 25] == 1)
        //                {
        //                    m.AluminiumWeldPositionResult1 = "OK";
        //                }
        //                else if (m.list_Data1[i, 25] == 2)
        //                {
        //                    m.AluminiumWeldPositionResult1 = "NG";
        //                }
        //                else
        //                {
        //                    m.AluminiumWeldPositionResult1 = "OK";
        //                }
        //                //芯包铝焊印位置判定结果2
        //                if (m.list_Data1[i, 27] == 1)
        //                {
        //                    m.AluminiumWeldPositionResult2 = "OK";
        //                }
        //                else if (m.list_Data1[i, 27] == 2)
        //                {
        //                    m.AluminiumWeldPositionResult2 = "NG";
        //                }
        //                else
        //                {
        //                    m.AluminiumWeldPositionResult2 = "OK";
        //                }

        //                m.CSBYHASCANNINGTIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        //                //m.RingA = m.list_Data1[i, 0];//无环轨编号，不显示
        //                m.CSBYHWEIGHTNOA1 = m.list_Data1[i, 1];
        //                m.CSBYHTHICKNESSTESTSTATIONNOA = m.list_Data1[i, 2];
        //                m.CSBYHTOOLNOA = m.list_Data1[i, 3];
        //                m.CSBYHCUWELDINGSTATIONNOA1 = m.list_Data1[i, 4];
        //                m.CSBYHCUWELDINGSTATIONNOA2 = m.list_Data1[i, 5];
        //                m.CSBYHCUWELDINGSTATIONNOA3 = m.list_Data1[i, 6];
        //                m.CSBYHALWELDINGSTATIONNOA1 = m.list_Data1[i, 7];
        //                m.CSBYHALWELDINGSTATIONNOA2 = m.list_Data1[i, 8];
        //                m.CSBYHALWELDINGSTATIONNOA3 = m.list_Data1[i, 9];
        //                m.CSBYHCUTABCUTTERNOA = m.list_Data1[i, 21];
        //                m.CSBYHALTABCUTTERNOA = m.list_Data1[i, 22];

        //                m.CSBYHCUWELDHEADCOUNTA1 = m.list_Data2[i, 0];
        //                m.CSBYHCUWELDHEADCOUNTA2 = m.list_Data2[i, 2];
        //                m.CSBYHCUWELDHEADCOUNTA3 = m.list_Data2[i, 4];
        //                m.CSBYHCUWELDBASECOUNTA1 = m.list_Data2[i, 1];
        //                m.CSBYHCUWELDBASECOUNTA2 = m.list_Data2[i, 3];
        //                m.CSBYHCUWELDBASECOUNTA3 = m.list_Data2[i, 5];

        //                m.CSBYHALWELDHEADCOUNTA1 = m.list_Data2[i, 6];
        //                m.CSBYHALWELDHEADCOUNTA2 = m.list_Data2[i, 8];
        //                m.CSBYHALWELDHEADCOUNTA3 = m.list_Data2[i, 10];
        //                m.CSBYHALWELDBASECOUNTA1 = m.list_Data2[i, 7];
        //                m.CSBYHALWELDBASECOUNTA2 = m.list_Data2[i, 9];
        //                m.CSBYHALWELDBASECOUNTA3 = m.list_Data2[i, 11];
        //                m.CSBYHCUTABCUTTERCOUNTA = m.list_Data2[i, 12];
        //                m.CSBYHALTABCUTTERCOUNTA = m.list_Data2[i, 13];
        //                m.CopperWeldHeadLife = m.list_Data2[i, 14];
        //                m.CopperWeldSeatLife = m.list_Data2[i, 15];
        //                m.AluminiumWeldHeadLife = m.list_Data2[i, 16];
        //                m.AluminiumWeldSeatLife = m.list_Data2[i, 17];
        //                m.CuttingKnifeLife = m.list_Data2[i, 18];

        //                m.CSBYHPROCESSNAME = "超声波预焊接";
        //                m.CSBYHSCHEDULE = CommonMethods.strClass;
        //                m.CSBYHINSTATIONTIME = m.ScanTime[0];
        //                m.CSBYHOUTSTATIONTIME = m.ScanTime[1];

        //                m.CSBYHWEIGHTA1MAXSETTING = SaveTwoNum(m.list_Data3[i, 0]);
        //                m.CSBYHWEIGHTA1MINSETTING = SaveTwoNum(m.list_Data3[i, 1]);
        //                m.CSBYHTHICKNESSTESTPRESSUREMAXSETTINGA = SaveTwoNum(m.list_Data3[i, 2]);
        //                m.CSBYHTHICKNESSTESTPRESSUREMINSETTINGA = SaveTwoNum(m.list_Data3[i, 3]);
        //                m.CSBYHTHICKNESSTESTPRESSUREMAXSIZEA = SaveTwoNum(m.list_Data3[i, 4]);
        //                m.CSBYHTHICKNESSTESTPRESSUREMINSIZEA = SaveTwoNum(m.list_Data3[i, 5]);
        //                m.CSBYHCUWELDINGPRESSUREMAXSETTING1 = SaveTwoNum(m.list_Data3[i, 6]);//压力最大
        //                m.CSBYHCUWELDINGPRESSUREMINSETTING = SaveTwoNum(m.list_Data3[i, 7]);//压力最小值
        //                m.CSBYHCUWELDINGPRESSUREMAXSETTING2 = SaveTwoNum(m.list_Data3[i, 14]);//压力只有一个设定值，显示该值
        //                m.CSBYHCUWELDINGPRESSUREMAXSETTING3 = SaveTwoNum(m.list_Data3[i, 22]);//压力只有一个设定值，显示该值
        //                m.CSBYHCUWELDINGPOWERMAXSETTING1 = SaveTwoNum(m.list_Data3[i, 8]);
        //                m.CSBYHCUWELDINGPOWERMAXSETTING2 = SaveTwoNum(m.list_Data3[i, 16]);
        //                m.CSBYHCUWELDINGPOWERMAXSETTING3 = SaveTwoNum(m.list_Data3[i, 24]);
        //                m.CSBYHCUWELDINGPOWERMINSETTING1 = SaveTwoNum(m.list_Data3[i, 9]);
        //                m.CSBYHCUWELDINGPOWERMINSETTING2 = SaveTwoNum(m.list_Data3[i, 17]);
        //                m.CSBYHCUWELDINGPOWERMINSETTING3 = SaveTwoNum(m.list_Data3[i, 25]);
        //                m.CSBYHCUWELDINGENERGYMAXSETTING1 = SaveTwoNum(m.list_Data3[i, 10]);
        //                m.CSBYHCUWELDINGENERGYMAXSETTING2 = SaveTwoNum(m.list_Data3[i, 18]);
        //                m.CSBYHCUWELDINGENERGYMAXSETTING3 = SaveTwoNum(m.list_Data3[i, 26]);
        //                m.CSBYHCUWELDINGENERGYMINSETTING1 = SaveTwoNum(m.list_Data3[i, 11]);
        //                m.CSBYHCUWELDINGENERGYMINSETTING2 = SaveTwoNum(m.list_Data3[i, 19]);
        //                m.CSBYHCUWELDINGENERGYMINSETTING3 = SaveTwoNum(m.list_Data3[i, 27]);
        //                m.CSBYHCUWELDINGAMPLITUDEMAXSETTING1 = SaveTwoNum(m.list_Data3[i, 12]);
        //                m.CSBYHCUWELDINGAMPLITUDEMAXSETTING2 = SaveTwoNum(m.list_Data3[i, 20]);
        //                m.CSBYHCUWELDINGAMPLITUDEMAXSETTING3 = SaveTwoNum(m.list_Data3[i, 28]);
        //                m.CSBYHCUWELDINGAMPLITUDEMINSETTING1 = SaveTwoNum(m.list_Data3[i, 13]);
        //                m.CSBYHCUWELDINGAMPLITUDEMINSETTING2 = SaveTwoNum(m.list_Data3[i, 21]);
        //                m.CSBYHCUWELDINGAMPLITUDEMINSETTING3 = SaveTwoNum(m.list_Data3[i, 29]);

        //                m.CSBYHALWELDINGPRESSUREMAXSETTING1 = SaveTwoNum(m.list_Data3[i, 30]);//
        //                m.CSBYHALWELDINGPRESSUREMINSETTING = SaveTwoNum(m.list_Data3[i, 31]);//
        //                m.CSBYHALWELDINGPRESSUREMAXSETTING2 = SaveTwoNum(m.list_Data3[i, 38]);//
        //                m.CSBYHALWELDINGPRESSUREMAXSETTING3 = SaveTwoNum(m.list_Data3[i, 46]);//
        //                m.CSBYHALWELDINGPOWERMAXSETTING1 = SaveTwoNum(m.list_Data3[i, 32]);
        //                m.CSBYHALWELDINGPOWERMAXSETTING2 = SaveTwoNum(m.list_Data3[i, 40]);
        //                m.CSBYHALWELDINGPOWERMAXSETTING3 = SaveTwoNum(m.list_Data3[i, 48]);
        //                m.CSBYHALWELDINGPOWERMINSETTING1 = SaveTwoNum(m.list_Data3[i, 33]);
        //                m.CSBYHALWELDINGPOWERMINSETTING2 = SaveTwoNum(m.list_Data3[i, 41]);
        //                m.CSBYHALWELDINGPOWERMINSETTING3 = SaveTwoNum(m.list_Data3[i, 49]);
        //                m.CSBYHALWELDINGENERGYMAXSETTING1 = SaveTwoNum(m.list_Data3[i, 34]);
        //                m.CSBYHALWELDINGENERGYMAXSETTING2 = SaveTwoNum(m.list_Data3[i, 42]);
        //                m.CSBYHALWELDINGENERGYMAXSETTING3 = SaveTwoNum(m.list_Data3[i, 50]);
        //                m.CSBYHALWELDINGENERGYMINSETTING1 = SaveTwoNum(m.list_Data3[i, 35]);
        //                m.CSBYHALWELDINGENERGYMINSETTING2 = SaveTwoNum(m.list_Data3[i, 43]);
        //                m.CSBYHALWELDINGENERGYMINSETTING3 = SaveTwoNum(m.list_Data3[i, 51]);
        //                m.CSBYHALWELDINGAMPLITUDEMAXSETTING1 = SaveTwoNum(m.list_Data3[i, 36]);
        //                m.CSBYHALWELDINGAMPLITUDEMAXSETTING2 = SaveTwoNum(m.list_Data3[i, 44]);
        //                m.CSBYHALWELDINGAMPLITUDEMAXSETTING3 = SaveTwoNum(m.list_Data3[i, 52]);
        //                m.CSBYHALWELDINGAMPLITUDEMINSETTING1 = SaveTwoNum(m.list_Data3[i, 37]);
        //                m.CSBYHALWELDINGAMPLITUDEMINSETTING2 = SaveTwoNum(m.list_Data3[i, 45]);
        //                m.CSBYHALWELDINGAMPLITUDEMINSETTING3 = SaveTwoNum(m.list_Data3[i, 53]);
        //                //m.pan = m.list_pan[i, 0];
        //                m.CSBYHWEIGHTA1 = SaveTwoNum(m.list_Data4[i, 0]);
        //                m.CSBYHTHICKNESSTESTPRESSUREA = SaveTwoNum(m.list_Data4[i, 1]);
        //                m.CSBYHTHICKNESSTESTPRESSURESIZEA = SaveTwoNum(m.list_Data4[i, 2]);
        //                m.CSBYHCUTABLENGTHA = SaveTwoNum(m.list_Data4[i, 3]);
        //                m.CSBYHCUTABWIDTHA = SaveTwoNum(m.list_Data4[i, 4]);
        //                m.CSBYHALTABLENGTHA = SaveTwoNum(m.list_Data4[i, 5]);
        //                m.CSBYHALTABWIDTHA = SaveTwoNum(m.list_Data4[i, 6]);
        //                m.CSBYHCUTADHESIONRESULTA = SaveTwoNum(m.list_Data4[i, 7]);
        //                m.CSBYHCUTBURRRESULTA = SaveTwoNum(m.list_Data4[i, 8]);
        //                m.CSBYHTOPTOSOLDERDISTANCEA = SaveTwoNum(m.list_Data4[i, 9]);
        //                m.CSBYHTOPTOSOLDERDISTANCEA2 = SaveTwoNum(m.list_Data4[i, 45]);
        //                m.CSBYHSOLDERTOEARDISTANCEA = SaveTwoNum(m.list_Data4[i, 10]);
        //                m.CSBYHFRONTEARCRACKLERESULTA = SaveTwoNum(m.list_Data4[i, 11]);
        //                m.CSBYHBACKEARCRACKLERESULTA = SaveTwoNum(m.list_Data4[i, 12]);
        //                m.AluminumToWeld1 = SaveTwoNum(m.list_Data4[i, 44]);
        //                m.AluminumToWeld2 = SaveTwoNum(m.list_Data4[i, 46]);

        //                m.CSBYHCUPRESSUREA1 = SaveTwoNum(m.list_Data4[i, 13]);
        //                m.CSBYHCUPOWERA1 = SaveTwoNum(m.list_Data4[i, 14]);
        //                m.CSBYHCUENERGYA1 = SaveTwoNum(m.list_Data4[i, 15]);
        //                m.CSBYHCUAMPLITUDEA1 = SaveTwoNum(m.list_Data4[i, 16]);
        //                m.CSBYHCUTIMEA1 = SaveTwoNum(m.list_Data4[i, 17]);
        //                m.CSBYHCUPRESSUREA2 = SaveTwoNum(m.list_Data4[i, 18]);
        //                m.CSBYHCUPOWERA2 = SaveTwoNum(m.list_Data4[i, 19]);
        //                m.CSBYHCUENERGYA2 = SaveTwoNum(m.list_Data4[i, 20]);
        //                m.CSBYHCUAMPLITUDEA2 = SaveTwoNum(m.list_Data4[i, 21]);
        //                m.CSBYHCUTIMEA2 = SaveTwoNum(m.list_Data4[i, 22]);
        //                m.CSBYHCUPRESSUREA3 = SaveTwoNum(m.list_Data4[i, 23]);
        //                m.CSBYHCUPOWERA3 = SaveTwoNum(m.list_Data4[i, 24]);
        //                m.CSBYHCUENERGYA3 = SaveTwoNum(m.list_Data4[i, 25]);
        //                m.CSBYHCUAMPLITUDEA3 = SaveTwoNum(m.list_Data4[i, 26]);
        //                m.CSBYHCUTIMEA3 = SaveTwoNum(m.list_Data4[i, 27]);

        //                m.CSBYHALPRESSUREA1 = SaveTwoNum(m.list_Data4[i, 28]);
        //                m.CSBYHALPOWERA1 = SaveTwoNum(m.list_Data4[i, 29]);
        //                m.CSBYHALENERGYA1 = SaveTwoNum(m.list_Data4[i, 30]);
        //                m.CSBYHALAMPLITUDEA1 = SaveTwoNum(m.list_Data4[i, 31]);
        //                m.CSBYHALTIMEA1 = SaveTwoNum(m.list_Data4[i, 32]);
        //                m.CSBYHALPRESSUREA2 = SaveTwoNum(m.list_Data4[i, 33]);
        //                m.CSBYHALPOWERA2 = SaveTwoNum(m.list_Data4[i, 34]);
        //                m.CSBYHALENERGYA2 = SaveTwoNum(m.list_Data4[i, 35]);
        //                m.CSBYHALAMPLITUDEA2 = SaveTwoNum(m.list_Data4[i, 36]);
        //                m.CSBYHALTIMEA2 = SaveTwoNum(m.list_Data4[i, 37]);
        //                m.CSBYHALPRESSUREA3 = SaveTwoNum(m.list_Data4[i, 38]);
        //                m.CSBYHALPOWERA3 = SaveTwoNum(m.list_Data4[i, 39]);
        //                m.CSBYHALENERGYA3 = SaveTwoNum(m.list_Data4[i, 40]);
        //                m.CSBYHALAMPLITUDEA3 = SaveTwoNum(m.list_Data4[i, 41]);
        //                m.CSBYHALTIMEA3 = SaveTwoNum(m.list_Data4[i, 42]);
        //                m.ThicknessMeasureDisplacement = m.list_Data4[i, 43];

        //                //条码，芯包重量，芯包侧厚，芯包CCD极性检测，电芯裁切粘连，电芯裁切毛刺，芯包焊印位置，
        //                //芯包正面极耳撕裂，芯包背面极耳撕裂，芯包正面极耳翻折，芯包背面极耳翻折
        //                MyRes[i] = JudgmentResult(Code_NG, m.list_Data1[i, 10], m.list_Data1[i, 11],
        //                    m.list_Data1[i, 12], m.list_Data1[i, 13], m.list_Data1[i, 14], m.list_Data1[i, 15],
        //                    m.list_Data1[i, 16], m.list_Data1[i, 17], m.list_Data1[i, 18], m.list_Data1[i, 19],
        //                    m.list_Data1[i, 23], m.list_Data1[i, 24], m.list_Data1[i, 25]);

        //                m.Result = MyRes[i];

        //                m.CSBYHOUTSTATIONJUDGMENTRESULT = m.judgment_result[i] = OK_NG; ;

        //                m.Remark += NG_Type;

        //                NG_Type = "";//清除上一次不良字符串
        //                OK_NG = ""; //清除上一次结果

        //                list.Add(m);

        //                string ur2 = CommonMethods.mesConfig.ResultProcessParamMesUrl;
        //                string siteCode1 = CommonMethods.mesConfig.siteCode;
        //                string lineCode1 = CommonMethods.mesConfig.lineCode;
        //                string equipNum1 = CommonMethods.mesConfig.equipNum;
        //                string materialCode1 = CommonMethods.mesConfig.MaterialCode;
        //                string userName1 = CommonMethods.mesConfig.mesUserName;
        //                MesUploadType unloadType = MesUploadType.DD;
        //                string Mesage = "";
        //                //加判断无条码不上传mes  1为有条码
        //                if (Code_NG == 1)
        //                {
        //                    string MesMessage = "";
        //                    MesResType mesResType = MesResType.C;
        //                    if (CommonMethods.mesConfig.isUpMes)
        //                    {
        //                        string url = CommonMethods.mesConfig.OutStationMesUrl;
        //                        string siteCode = CommonMethods.mesConfig.siteCode;
        //                        string lineCode = CommonMethods.mesConfig.lineCode;
        //                        string equipNum = CommonMethods.mesConfig.equipNum;
        //                        string materialCode = CommonMethods.mesConfig.MaterialCode;
        //                        string userName = CommonMethods.mesConfig.mesUserName;
        //                        int type = 1;
        //                        //int type = 1;
        //                        //string materialLotCodeLocator = "";

        //                        bool Res = CommonMethods.hbgMes.OutboundInformations(url, siteCode, lineCode, equipNum, materialCode, userName, m, ref MesMessage, ref mesResType, NGcodeList);
        //                        //bool Res = CommonMethods.hbgMes.SingleCellOutStation(url, siteCode, lineCode, equipNum, materialCode, userName, m,)
        //                        if (MesMessage.Contains("发生一个或多个错误"))
        //                        {
        //                            MesMessage = "上传失败,";
        //                        }
        //                        else
        //                        {
        //                            m.Flag = 1;
        //                        }
        //                        if (Res)
        //                        {
        //                            //MES出站判定结果
        //                            m.Result = MyRes[i];
        //                            //芯包重量1判定结果
        //                            if (m.list_Data1[i, 10] == 2) { m.Remark += "芯包重量NG,"; }
        //                            //芯包侧厚判定结果
        //                            if (m.list_Data1[i, 11] == 2) { m.Remark += "芯包侧厚NG,"; }
        //                            //芯包CCD极性检测结果
        //                            if (m.list_Data1[i, 12] == 2) { m.Remark += "芯包CCD极性检测NG,"; }
        //                            m.Remark += MesMessage;
        //                        }
        //                        else
        //                        {
        //                            switch (mesResType)
        //                            {
        //                                case MesResType.A:// 停线，设备报警停机
        //                                    //m.listRes[i] = 10;
        //                                    m.Result = MyRes[i] = 2;
        //                                    //m.Remark = MesMessage[i];
        //                                    break;
        //                                case MesResType.B://不停机，MEs NG品要排出，要给PLC发NG信号
        //                                    //m.listRes[i] = 9;
        //                                    m.Result = MyRes[i] = 2;
        //                                    //m.Remark = MesMessage[i];
        //                                    break;
        //                                case MesResType.C://可忽略
        //                                    //m.listRes[i] = 1;
        //                                    m.Result = MyRes[i] = 2;
        //                                    //m.Remark = MesMessage[i];
        //                                    break;
        //                                default:
        //                                    m.Result = MyRes[i] = 2;
        //                                    break;
        //                            }
        //                            //芯包重量1判定结果
        //                            if (m.list_Data1[i, 10] == 2) { m.Remark += "芯包重量NG,"; }
        //                            //芯包侧厚判定结果
        //                            if (m.list_Data1[i, 11] == 2) { m.Remark += "芯包侧厚NG,"; }
        //                            //芯包CCD极性检测结果
        //                            if (m.list_Data1[i, 12] == 2) { m.Remark += "芯包CCD极性检测NG,"; }
        //                            m.Remark += MesMessage;
        //                        }

        //                        bool Res1 = CommonMethods.hbgMes.ProducResultParam(ur2, siteCode1, lineCode1, equipNum1, materialCode1, userName1, m, unloadType, ref Mesage);


        //                        if (m.Flag == 1)
        //                            m.UpState = "已上传";
        //                        else
        //                            m.UpState = "未上传";
        //                    }
        //                    else
        //                    {
        //                        m.Result = MyRes[i];
        //                        m.Remark += "本地存储";
        //                        m.UpState = "未上传";
        //                    }
        //                    if (CommonMethods.sysConfig.OKSwitching)
        //                    {
        //                        //调试发OK
        //                        m.Result = MyRes[i] = 1;
        //                        //m.Result = "OK";
        //                    }
        //                }
        //                else
        //                {
        //                    m.Result = MyRes[i] = 2;
        //                    m.Remark += "本地存储";
        //                    //芯包重量1判定结果
        //                    if (m.list_Data1[i, 10] == 2) { m.Remark += ",芯包重量NG"; }
        //                    //芯包侧厚判定结果
        //                    if (m.list_Data1[i, 11] == 2) { m.Remark += ",芯包侧厚NG"; }
        //                    //芯包CCD极性检测结果
        //                    if (m.list_Data1[i, 12] == 2) { m.Remark += ",芯包CCD极性检测NG"; }
        //                    m.UpState = "未上传";
        //                }
        //                //极限件发3给PLC
        //                if (ChallengeFlag == 1)
        //                {
        //                    if (string.Compare(EX[0].WeldingCCD, "焊前CCD") == 0 || string.Compare(EX[0].WeldingCCD, "焊前ccd") == 0)
        //                    {
        //                        MyRes[i] = 3;
        //                    }
        //                    if (string.Compare(EX[0].WeldingCCD, "焊后CCD") == 0 || string.Compare(EX[0].WeldingCCD, "焊后ccd") == 0)
        //                    {
        //                        MyRes[i] = 4;
        //                    }
        //                }
        //            }
        //            catch (Exception ex)
        //            {
        //                LogHelper.Instance.WriteError($"外观检测出站线程数据解析：{ex}", "SysErrorLog");
        //            }
        //        }

        //        #endregion
        //        JYResult result1 = null;
        //        JYResult result2 = null;
        //        if (pf.IsFeedbackPlc)
        //        {
        //            //result1 = CommonMethods.PlcLink.WriteValue(pf.VarList[2].VarAddress, MyRes[0], Data_Type.Short);//判断结果写入PLC
        //            //result2 = CommonMethods.PlcLink.WriteValue(pf.VarList[3].VarAddress, MyRes[1], Data_Type.Short);//判断结果写入PLC
        //            //if (CommonMethods.sysConfig.OKSwitching)
        //            {
        //                //调试发OK
        //                //MyRes[0] = 1;
        //                //MyRes[1] = 1;
        //                Thread.Sleep(500);
        //                result1 = CommonMethods.PlcLink.WriteValue(pf.VarList[2].VarAddress, MyRes[0], Data_Type.Short);//判断结果写入PLC
        //                result2 = CommonMethods.PlcLink.WriteValue(pf.VarList[3].VarAddress, MyRes[1], Data_Type.Short);//判断结果写入PLC
        //            }
        //            //else
        //            //{
        //            //    //将mes反馈的结果发给PLC
        //            //    Thread.Sleep(500);
        //            //    result1 = CommonMethods.PlcLink.WriteValue(pf.VarList[2].VarAddress, MyRes[0], Data_Type.Short);//判断结果写入PLC
        //            //    result2 = CommonMethods.PlcLink.WriteValue(pf.VarList[3].VarAddress, MyRes[1], Data_Type.Short);//判断结果写入PLC
        //            //}
        //        }
        //        JYResult result3 = CommonMethods.PlcLink.WriteValue(pf.VarList[0].VarAddress, 0);//置位0
        //        CommonMethods.AddDataMonitorLog(0, $"发送信息给PLC:{result1.IsSuccess},{MyRes[0]},{MyRes[1]}");
        //        LogHelper.Instance.WriteLog($"发送信息给PLC:{result1.IsSuccess},{MyRes[0]},{MyRes[1]}");

        //        Task.Run(() => { SendDateMQTT.Instance.EnterandExitSendMessge(1); });
        //        //数据存储 - 异步处理
        //        Task.Run(() =>
        //        {
        //            SaveBlankingData(list);
        //        });

        //        sw2.Stop();
        //        ////输出下料数据处理耗时：
        //        CommonMethods.AddDataMonitorLog(0, $"外观检测出站共耗时：{sw2.Elapsed.TotalMilliseconds}ms");
        //    }
        //}
        #endregion
        //下料解析 外观检
        private void BlankingStation(JYResult<byte[]> bArrays, object plcModel)
        {
            Group pf = plcModel as Group;
            if (bArrays == null)
            {
                CommonMethods.PlcLink.WriteInt16(pf.VarList[0].VarAddress, 0);//置位0
                CommonMethods.AddLog(false, $"电芯出站PLC有触发信号，但无数据!");
                LoggerHelp.WriteLog($"电芯出站PLC有触发信号，但无数据!", "SysErrorLog");
            }
            if (bArrays.IsSuccess && bArrays.Content != null)
            {
                Random random = new Random();
                //解析数据  
                sw2.Restart();
                #region 数据变量定义
                List<BGearEntity> list = new List<BGearEntity>();//实体
                //int[] MyRes = new int[2];//AB反馈结果
                int[] MyRes = new int[6];//6个外观检测反馈结果
                #endregion
                //高低位转换
                byte[] bytes = bArrays.Content.ByteReverse();
                byte[] bytes1 = bArrays.Content;

                List<string> listBarCode = GetList<string>(6, "");//电芯条码
                //List<string> listNum = GetList<string>(1, "");//进站磁悬浮动子编号
                List<short> listRes = GetList<short>(6, 0);//出站电芯反馈结果
                List<short> listGradeResult = GetList<short>(6, 0);//grading plc 数字结果
                //反馈结果
                #region 电芯解析
                //电芯解析  从3200 开始              
                listBarCode[0] = StringLib.GetStringFromByteArrayByEncoding(bytes, 0, 40, System.Text.Encoding.ASCII).Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
                listBarCode[1] = StringLib.GetStringFromByteArrayByEncoding(bytes, 40, 40, System.Text.Encoding.ASCII).Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
                listBarCode[2] = StringLib.GetStringFromByteArrayByEncoding(bytes, 80, 40, System.Text.Encoding.ASCII).Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
                listBarCode[3] = StringLib.GetStringFromByteArrayByEncoding(bytes, 120, 40, System.Text.Encoding.ASCII).Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
                listBarCode[4] = StringLib.GetStringFromByteArrayByEncoding(bytes, 160, 40, System.Text.Encoding.ASCII).Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
                listBarCode[5] = StringLib.GetStringFromByteArrayByEncoding(bytes, 200, 40, System.Text.Encoding.ASCII).Replace('\0', ' ').Replace('\r', ' ').Replace(" ", "").Trim();
                #region 上传MES
                var lstBGearEntity = GetBlankingData(listBarCode);
                //D3200起
                for (int i = 0; i < 6; i++)
                {
                    //读取bgearentity
                   
                    try
                    {
                        BGearEntity m = new BGearEntity();
                        AGearEntity TT = new AGearEntity();
                        //极限件标志
                        int ChallengeFlag = 0;
                        var bGearEntity = lstBGearEntity.OrderByDescending(x => x.CSBYHOUTSTATIONTIME).FirstOrDefault(x => x.BarCode == listBarCode[i]);
                        if (bGearEntity != null)
                        {
                            m = bGearEntity;
                        }

                        //芯包扫码时间
                        //m.ScanTime[i] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");                       
                        m.BarCode = listBarCode[i];
                        //m.CSBYHCODEA = "TEST";
                        #region 极限件查询
                        //var EX = CommonMethods.db.QuerySqlList<Challenge>(CommonMethods.dBSQL.GetChallengeCode(m.BarCode));
                        //for (int j = 0; j < EX.Count; j++)
                        //{
                        //    if (string.Compare(EX[j].ChallengeBarCode, m.BarCode) == 0)
                        //    {
                        //        m.Remark += "(极限件," + EX[j].CodeMessage + "," + EX[j].WeldingCCD + ")";
                        //        ChallengeFlag = 1;
                        //    }
                        //}
                        #endregion
                        //查询进站时间数据
                        var w = CommonMethods.db.QuerySqlList<AGearEntity>(CommonMethods.dBSQL.GetInTime(m.BarCode));
                        //获取新一行数据
                        if (w.Count > 0)
                            TT = w[w.Count - 1];
                        //
                        //if (w.Count > 0)
                        //    TT = w[0];
                        if (TT.TestTime != null)
                            m.ScanTime[0] = TT.TestTime;
                        else
                            m.ScanTime[0] = "";
                        //芯包出站时间
                        m.ScanTime[1] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                        //复投次数
                        m.RethrowNum = 0;
                        if (w.Count > 0)
                        {
                            int cccc = w.Count;
                            m.RethrowNum = cccc - 1;
                        }

                        int Code_NG = 0;//条码有无标志 1为有，2为无
                        //判断有无条码
                        if (m.BarCode != null && m.BarCode != "" && m.BarCode != "ERROR")
                            Code_NG = 1;
                        else
                            Code_NG = 2;

                        //m.ChannelNo = m.MyNo[i];
                        //m.TestTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                        //不良代码判定
                        //List<string> NGcodeList = new List<string>();
                        //NGcodeList = NGcode(m.list_Data1[i, 10], m.list_Data1[i, 11], m.list_Data1[i, 12], m.list_Data1[i, 15], m.list_Data1[i, 26], m.list_Data1[i, 16]
                        //    , m.list_Data1[i, 17], m.list_Data1[i, 18], m.list_Data1[i, 19], m.list_Data1[i, 23], m.list_Data1[i, 24], m.list_Data1[i, 25], m.list_Data1[i, 27]);
                       // var (NGcodeList, NGmessage) = NGWGJcode(m.CCD_Data, i);

                        //电芯CCD判定结果
                   
                        m.CSBYHASCANNINGTIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        m.CSBYHPROCESSNAME = "分档";
                        m.CSBYHSCHEDULE = CommonMethods.strClass;
                        m.CSBYHINSTATIONTIME = m.ScanTime[0];
                        m.CSBYHOUTSTATIONTIME = m.ScanTime[1];

                       
                        m.ChannelNo = i + 1;
                        //条码，芯包重量，芯包侧厚，芯包CCD极性检测，电芯裁切粘连，电芯裁切毛刺，芯包焊印位置，
                        //芯包正面极耳撕裂，芯包背面极耳撕裂，芯包正面极耳翻折，芯包背面极耳翻折

                        MyRes[i] = m.Result;
                       
                        m.MaterialCode = CommonMethods.mesConfig.MaterialCode;
                        NG_Type = "";//清除上一次不良字符串
                        OK_NG = ""; //清除上一次结果

                        list.Add(m);

                       
                        string ur2 = CommonMethods.mesConfig.ResultProcessParamMesUrl;
                        string siteCode1 = CommonMethods.mesConfig.siteCode;
                        string lineCode1 = CommonMethods.mesConfig.lineCode;
                        string equipNum1 = CommonMethods.mesConfig.equipNum;
                        string materialCode1 = CommonMethods.mesConfig.MaterialCode;
                        string userName1 = CommonMethods.mesConfig.mesUserName;
                        MesUploadType unloadType = MesUploadType.DD;
                        string Mesage = "";
                        //加判断无条码不上传mes  1为有条码
                        #region  发送结果MES ,本地存储
                        if (Code_NG == 1)
                        {
                            string MesMessage = "";
                            MesResType mesResType = MesResType.C;
                           
                            if (CommonMethods.mesConfig.isUpMes)
                            {
                                #region 出站  分档取消出站
                                ////{
                                ////    string url = CommonMethods.mesConfig.OutStationMesUrl;
                                ////    string siteCode = CommonMethods.mesConfig.siteCode;
                                ////    string lineCode = CommonMethods.mesConfig.lineCode;
                                ////    string equipNum = CommonMethods.mesConfig.equipNum;
                                ////    string materialCode = CommonMethods.mesConfig.MaterialCode;
                                ////    string userName = CommonMethods.mesConfig.mesUserName;
                                ////    int type = 1;
                                ////    //int type = 1;
                                ////    //string materialLotCodeLocator = "";
                                ////    var (NGcodeList, NGmessage) = NGWGJcode(m.CCD_NgNo, i);
                                ////    bool Res = CommonMethods.hbgMes.OutboundInformations(url, siteCode, lineCode, equipNum, materialCode, userName, m, ref MesMessage, ref mesResType, NGcodeList, NGmessage);
                                ////    //bool Res = CommonMethods.hbgMes.SingleCellOutStation(url, siteCode, lineCode, equipNum, materialCode, userName, m,)
                                ////    if (MesMessage.Contains("发生一个或多个错误"))
                                ////    {
                                ////        MesMessage = "上传失败,";
                                ////    }
                                ////    else
                                ////    {
                                ////        m.Flag = 1;
                                ////    }
                                ////    if (Res)
                                ////    {
                                ////        //MES出站判定结果
                                ////        m.Result = MyRes[i];
                                ////        ////芯包重量1判定结果
                                ////        //if (m.list_Data1[i, 10] == 2) { m.Remark += "芯包重量NG,"; }
                                ////        ////芯包侧厚判定结果
                                ////        //if (m.list_Data1[i, 11] == 2) { m.Remark += "芯包侧厚NG,"; }
                                ////        ////芯包CCD极性检测结果
                                ////        //if (m.list_Data1[i, 12] == 2) { m.Remark += "芯包CCD极性检测NG,"; }
                                ////        m.Remark += MesMessage;
                                ////    }
                                ////    else
                                ////    {
                                ////        switch (mesResType)
                                ////        {
                                ////            case MesResType.A:// 停线，设备报警停机
                                ////                              //m.listRes[i] = 10;
                                ////                              // m.Result = MyRes[i] = 2;
                                ////                m.Result = MyRes[i] = 3;
                                ////                m.Remark += "MES NG";
                                ////                break;
                                ////            case MesResType.B://不停机，MEs NG品要排出，要给PLC发NG信号
                                ////                              //m.listRes[i] = 9;
                                ////                              // m.Result = MyRes[i] = 2;
                                ////                m.Result = MyRes[i] = 3;
                                ////                m.Remark += "MES NG";
                                ////                break;
                                ////            case MesResType.C://可忽略 
                                ////                              //m.listRes[i] = 1;
                                ////                              // m.Result = MyRes[i] = 2;
                                ////                m.Result = MyRes[i] = 1;
                                ////                m.Remark += "MES Alerm OK";
                                ////                break;
                                ////            case MesResType.D://可忽略 
                                ////                              //m.listRes[i] = 1;
                                ////                              // m.Result = MyRes[i] = 2;
                                ////                m.Result = MyRes[i] = 1;
                                ////                m.Remark += "MES OK";
                                ////                break;
                                ////            default:
                                ////                m.Remark += "MES NG";
                                ////                m.Result = MyRes[i] = 3;
                                ////                break;
                                ////        }
                                ////        ////芯包重量1判定结果
                                ////        //if (m.list_Data1[i, 10] == 2) { m.Remark += "芯包重量NG,"; }
                                ////        ////芯包侧厚判定结果
                                ////        //if (m.list_Data1[i, 11] == 2) { m.Remark += "芯包侧厚NG,"; }
                                ////        ////芯包CCD极性检测结果
                                ////        //if (m.list_Data1[i, 12] == 2) { m.Remark += "芯包CCD极性检测NG,"; }
                                ////        m.Remark += MesMessage;
                                ////    }
                                ////}
                                #endregion
                                //发送产品结果参数给MES
                                //bool Res1 = CommonMethods.hbgMes.ProducResultParam(ur2, siteCode1, lineCode1, equipNum1, materialCode1, userName1, m, unloadType, ref Mesage);


                                if (m.Flag == 1)
                                    m.UpState = "已上传";
                                else
                                    m.UpState = "未上传";
                            }
                            else
                            {
                                m.Result = MyRes[i];
                                m.Remark += "本地存储";
                                m.UpState = "未上传";
                            }
                           
                            if (CommonMethods.sysConfig.OKSwitching)
                            {
                                //调试发OK
                                m.Result = MyRes[i] = 1;
                                //m.Result = "OK";
                            }
                        }
                        else
                        {
                            m.Result = MyRes[i] = 2;
                            m.Remark += "本地存储";
                            ////芯包重量1判定结果
                            //if (m.list_Data1[i, 10] == 2) { m.Remark += ",芯包重量NG"; }
                            ////芯包侧厚判定结果
                            //if (m.list_Data1[i, 11] == 2) { m.Remark += ",芯包侧厚NG"; }
                            ////芯包CCD极性检测结果
                            //if (m.list_Data1[i, 12] == 2) { m.Remark += ",芯包CCD极性检测NG"; }
                            m.UpState = "未上传";
                        }
                        #endregion                      
                        #region OK分档查询
                        //if (CommonMethods.sysConfig.MesModeSwitching && CommonMethods.mesConfig.isConnected)
                        {     //1 分选测厚度NG ，2 分选外观不良  21分选一档,22分选二档，23分选三档、24分选四档，25分档5 26  ==6

                            if (list[i].Result == 1)
                            {
                                if (CommonMethods.mesConfig.isUpMes)
                                {
                                    //分档接口
                                    //21~26 OK挡位
                                    var (success, mesage, mesResType, mesResFangAnType) = CommonMethods.hbgMes.MesGrading(
                                    CommonMethods.mesConfig.GradingMesUrl,
                                    list[i].BarCode,
                                    CommonMethods.mesConfig.siteCode,
                                    CommonMethods.mesConfig.lineCode,
                                    CommonMethods.mesConfig.mesUserName,
                                    CommonMethods.mesConfig.equipNum,
                                    CommonMethods.mesConfig.MaterialCode);//分档查询

                                    try
                                    {
                                        //listGradeResult[i] = (short)26;//默认分档OK
                                        list[i].Remark = "外观检OK分档";
                                        list[i].Mes_battery_Grade = mesResType[0];
                                        list[i].Mes_sorting_scheme = mesResFangAnType[0];
                                        if (success && mesResType[0] != "NULL")
                                        {

                                            if (mesResType[0].Equals(CommonMethods.mesConfig.Grading1))
                                            {
                                                listGradeResult[i] = (short)(CommonMethods.MESGradingSet[0] + 20);
                                                list[i].Remark = $"出站条码[{list[i].BarCode}]外观检OKMES反馈档位:{mesResType[0]}";
                                            }
                                            else if (mesResType[0].Equals(CommonMethods.mesConfig.Grading2))
                                            {
                                                listGradeResult[i] = (short)(CommonMethods.MESGradingSet[1] + 20);
                                                list[i].Remark = $"出站条码[{list[i].BarCode}]外观检OKMES反馈档位:{mesResType[0]}";
                                            }
                                            else if (mesResType[0].Equals(CommonMethods.mesConfig.Grading3))
                                            {
                                                listGradeResult[i] = (short)(CommonMethods.MESGradingSet[2] + 20);
                                                list[i].Remark = $"出站条码[{list[i].BarCode}]外观检OKMES反馈档位:{mesResType[0]}";
                                            }
                                            else if (mesResType[0].Equals(CommonMethods.mesConfig.Grading4))
                                            {
                                                listGradeResult[i] = (short)(CommonMethods.MESGradingSet[3] + 20);
                                                list[i].Remark = $"出站条码[{list[i].BarCode}]外观检OKMES反馈档位:{mesResType[0]}";
                                            }
                                            else if (mesResType[0].Equals(CommonMethods.mesConfig.Grading5))
                                            {
                                                listGradeResult[i] = (short)(CommonMethods.MESGradingSet[4] + 20);
                                                list[i].Remark = $"出站条码[{list[i].BarCode}]外观检OKMES反馈档位:{mesResType[0]}";
                                            }
                                            else if (mesResType[0].Equals(CommonMethods.mesConfig.Grading6))
                                            {
                                                listGradeResult[i] = (short)(CommonMethods.MESGradingSet[5] + 20);
                                                list[i].Remark = $"出站条码[{list[i].BarCode}]外观检OKMES反馈档位:{mesResType[0]}";
                                            }
                                            else
                                            {
                                                throw new Exception("MES未反馈正确档位");
                                            }

                                        }
                                        else
                                            throw new Exception("MES未反馈正确档位");

                                    }
                                    catch (Exception ex)
                                    {
                                        listGradeResult[i] = (short)(CommonMethods.MESGradingSet[5] + 20); //(short)26;

                                        list[i].Remark = $"出站条码[{list[i].BarCode}]外观检OK分档NG，MES未反馈正确档位";
                                        LoggerHelp.WriteLog("解析出站MES分档数据异常：{ex.Message}", "SysErrorLog");

                                    }
                                }
                                else
                                {
                                    list[i].Remark = $"出站条码[{list[i].BarCode}]外观检OK分档，MES断连";
                                    listGradeResult[i] = (short)(CommonMethods.MESGradingSet[5] + 20); //(short)26;
                                                                                                       // list[i].Result = "MES NG";
                                    CommonMethods.PlcLink.WriteValue("D80", 1, Data_Type.Short);
                                    //反馈MES结果

                                }

                            }
                            else
                            {
                                //厚度不良分档  1 测厚不良 2 外观不良
                                if (list[i].CCD19 != 1)
                                {
                                    listGradeResult[i] = 1;
                                    list[i].Remark = $"出站条码[{list[i].BarCode}]测厚度NG";
                                }
                                else
                                {
                                    //外观不良分档
                                    listGradeResult[i] = 2;
                                    list[i].Remark = $"出站条码[{list[i].BarCode}]外观检NG不分类";
                                }
                            }
                            listRes[i] = listGradeResult[i];
                            list[i].Result = listGradeResult[i];
                            MyRes[i] = listGradeResult[i];

                        }



                        #endregion
                        #region 结果参数
                        //发送产品结果参数给MES
                        if (CommonMethods.mesConfig.isUpMes)
                        {
                            bool Res1 = CommonMethods.hbgMes.ProducResultParam(ur2, siteCode1, lineCode1, equipNum1, materialCode1, userName1, m, unloadType, ref Mesage);
                        }
                        #endregion
                        #region//极限件发3给PLC
                        //if (ChallengeFlag == 1)
                        //{
                        //    if (string.Compare(EX[0].WeldingCCD, "焊前CCD") == 0 || string.Compare(EX[0].WeldingCCD, "焊前ccd") == 0)
                        //    {
                        //        MyRes[i] = 3;
                        //    }
                        //    if (string.Compare(EX[0].WeldingCCD, "焊后CCD") == 0 || string.Compare(EX[0].WeldingCCD, "焊后ccd") == 0)
                        //    {
                        //        MyRes[i] = 4;
                        //    }
                        //}
                        #endregion
                    }
                    catch (Exception ex)
                    {
                        LoggerHelp.WriteLog($"外观检测出站线程数据解析：{ex}", "SysErrorLog");
                    }
                }

                #endregion
                JYResult result1 = null;
                JYResult result2 = null;
                JYResult result3 = null;
                if (pf.IsFeedbackPlc)
                {
                    //result1 = CommonMethods.PlcLink.WriteValue(pf.VarList[2].VarAddress, MyRes[0], Data_Type.Short);//判断结果写入PLC
                    //result2 = CommonMethods.PlcLink.WriteValue(pf.VarList[3].VarAddress, MyRes[1], Data_Type.Short);//判断结果写入PLC
                   // if (CommonMethods.sysConfig.OKSwitching)
                    {
                        //调试发OK
                        //MyRes[0] = 1;
                        //MyRes[1] = 1;
                        Thread.Sleep(500);
                        result1 = CommonMethods.PlcLink.WriteValue(pf.VarList[2].VarAddress, MyRes[0], Data_Type.Short);//判断结果写入PLC
                        result2 = CommonMethods.PlcLink.WriteValue(pf.VarList[3].VarAddress, MyRes[1], Data_Type.Short);//判断结果写入PLC
                        result3 = CommonMethods.PlcLink.WriteValue(pf.VarList[4].VarAddress, MyRes[2], Data_Type.Short);//判断结果写入PLC
                    }
                    //else
                    //{
                    //    //将mes反馈的结果发给PLC
                    //    Thread.Sleep(500);
                    //    result1 = CommonMethods.PlcLink.WriteValue(pf.VarList[2].VarAddress, MyRes[0], Data_Type.Short);//判断结果写入PLC
                    //    result2 = CommonMethods.PlcLink.WriteValue(pf.VarList[3].VarAddress, MyRes[1], Data_Type.Short);//判断结果写入PLC
                    //}
                }
                JYResult result4 = CommonMethods.PlcLink.WriteValue(pf.VarList[0].VarAddress, 0);//置位0
                CommonMethods.AddDataMonitorLog(0, $"发送信息给PLC:{result1.IsSuccess},{MyRes[0]},{MyRes[1]},{MyRes[2]}");
                LoggerHelp.WriteLog($"发送信息给PLC:{result1.IsSuccess},{MyRes[0]},{MyRes[1]},{MyRes[2]}");

                //Task.Run(() => { SendDateMQTT.Instance.EnterandExitSendMessge(1); });
                //数据存储 - 异步处理
                Task.Run(() =>
                {
                    SaveBlankingData(list);
                });

                sw2.Stop();
                ////输出下料数据处理耗时：
                CommonMethods.AddDataMonitorLog(0, $"外观检测出站共耗时：{sw2.Elapsed.TotalMilliseconds}ms");
            }
        }
        /// <summary>
        /// 电池结果判断
        /// </summary>
        /// <param name="HJ_Result"></param>
        /// <param name="strErr"></param>
        /// <returns></returns>
        private ushort GetResult(AGearEntity m, ref string strErr)
        {
            //strErr = "";
            //bool b = m.YaLiValue <= 2 ? true : false;//压力值结果NG
            //if (b)
            //{
            //    strErr = "良品";
            //    return 1;
            //}
            //if (!b)
            //{
            //    strErr = "压力值NG";
            //    return 5;
            //}

            return 1;
        }

        #region 保存下料数据
        /// <summary>
        /// 保存下料数据到数据
        /// </summary>
        /// <param name="list"></param>
        private void SaveBlankingData(List<BGearEntity> list)
        {
            //数据的保存
            try
            {
                if (list != null && list.Count > 0)
                {
                    //数据显示
                    List<BGearEntity> Blist = list;
                    int i = 0;
                    string strRes = "";
                    foreach (BGearEntity m in list)
                    {
                        bool b = CommonMethods.db.AddReturnBool<BGearEntity>(m);
                       
                        if(!b)
                        {
                            strRes = ",不存储";
                        }
                        m.Remark = m.Remark + strRes;
                        Blist[i].Remark = m.Remark;
                        i++;
                    }

                    //数据显示
                    CommonMethods.ShowBlankingDelegate.Invoke(Blist);
                }
                else
                {
                    LoggerHelp.WriteLog($"电芯出站数据List为空", "SysErrorLog");
                }
            }
            catch (Exception ex)
            {
                LoggerHelp.WriteLog($"电芯出站数据保存异常：{ex}", "SysErrorLog");
            }
        }
        #endregion

        #endregion

        #region 创建空的数组List
        public List<T> GetList<T>(int num, T data)
        {
            List<T> list = new List<T>();
            for (int i = 0; i < num; i++)
            {
                list.Add(data);
            }
            return list;

        }

        public List<MesResType> GetMesType(int Num)
        {
            List<MesResType> mesResTypes = new List<MesResType>();
            for (int i = 0; i < Num; i++)
            {
                mesResTypes.Add(MesResType.UnKnow);
            }
            return mesResTypes;
        }

        /// <summary>
        /// PLC内批量写\0
        /// </summary>
        /// <param name="Num"></param>
        /// <returns></returns>
        public string GetString_0(int Num)
        {
            string ff = "";
            for (int i = 0; i < Num; i++)
            {
                ff = ff.Insert(ff.Length, "\0");
            }
            return ff;
        }

        public string GetBarCodeNum(int Num)
        {
            string CodeNo = "";
            int CodeLenth = Num.ToString().Length;

            switch (CodeLenth)
            {
                case 1:
                    return CodeNo = $"00000{Num}";
                case 2:
                    return CodeNo = $"0000{Num}";
                case 3:
                    return CodeNo = $"000{Num}";
                case 4:
                    return CodeNo = $"00{Num}";
                case 5:
                    return CodeNo = $"0{Num}";
                default:
                    return CodeNo = $"{Num}";
            }
        }
        #endregion

        #region 下料不良代码
        public List<string> NGcode(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, int k, int l, int m)
        {
            //芯包重量、测厚位移、极性、铜焊印位置1、铜焊印位置2、正面极耳撕裂、背面极耳撕裂、
            //正面极耳翻折、背面极耳翻折、测厚压力、焊前CCD、铝焊印位置1、铝焊印位置2
            List<string> NGcodelist = new List<string>();
            if (a == 2 || b == 2) NGcodelist.Add("ETPP120");
            if (j == 2) NGcodelist.Add("ETPP119");
            if (c == 2) NGcodelist.Add("ETPP005");
            if (d == 2 || e == 2 || l == 2 || m == 2) NGcodelist.Add("ETPP118");
            if (f == 2 || g == 2) NGcodelist.Add("ETPP106");
            if (h == 2 || i == 2) NGcodelist.Add("ETPP108");
            if (k == 2) NGcodelist.Add("ETPA111");

            return NGcodelist;
        }
        //下料不良代码 外观检
        public (List<string>, List<string>) NGWGJcode(int[,] lstResult, int index)
        {

            List<string> NGcodelist = new List<string>();
            List<string> NGmessagelist = new List<string>();
            for (int i = 0; i < 16; i++)
            {
                if (lstResult[index, i] != 0)
                {
                    var strCode = lstResult[index, i].ToString().PadLeft(3, '0');
                    NGcodelist.Add($"PFVA{strCode}");
                    var codeEntity = CommonMethods.listNgCodeEntity.FirstOrDefault(x => strCode == x.ngCode);
                    if (codeEntity != null)
                    {
                        NGmessagelist.Add(codeEntity.ngMessage);
                    }
                    else
                    {
                        NGmessagelist.Add("未知错误");
                    }
                }
            }

            return (NGcodelist, NGmessagelist);
        }
       
        #endregion

        #region NG下料不良代码
        public List<string> NGCarcode(int a, int b, int c)
        {
            //芯包重量、测厚位移、极性
            List<string> NGcodelist = new List<string>();
            if (a == 2) NGcodelist.Add("ETPP120");
            if (b == 2) NGcodelist.Add("ETPP120");
            if (c == 2) NGcodelist.Add("ETPP105");

            return NGcodelist;
        }
        #endregion
        #region 读取下料数据 sql数据库
        private  List<BGearEntity> GetBlankingData(List<string> listBarCode)
        {
            //数据的读取
            List<BGearEntity> listData = new List<BGearEntity>();
            try
            {
                if (listBarCode != null && listBarCode.Count > 0)
                {
                    listData = CommonMethods.dbServer.QuerySqlList<BGearEntity>(CommonMethods.dBSQL.GetBGearEntityByBarcode(listBarCode)).ToList();


                }

            }
            catch (Exception ex)
            {
                LoggerHelp.WriteLog($"GetBlankingData：{ex}", "SysErrorLog");
            }
            return listData;
        }
        /// <summary>
        /// 通过RFID读取下料数据 mysql数据库
        /// </summary>
        /// <param name="strRFID"></param>
        /// <returns></returns>
        //private List<BGearEntity> GetBlankingData_ByRFID(string strRFID)
        //{
        //    //数据的读取
        //    List<BGearEntity> listData = new List<BGearEntity>();
        //    try
        //    {
        //        if (!string.IsNullOrEmpty(strRFID) && strRFID != "ERROR")
        //        {
        //            var listDataTemp = CommonMethods.dbServer.QuerySqlList<BGearEntityTemp>(CommonMethods.dBSQL.GetBGearEntity(strRFID));
        //            if (listDataTemp != null && listDataTemp.Count > 0)
        //            {
        //                foreach (var m in listDataTemp)
        //                {
        //                    var entity = new BGearEntity();
        //                    foreach (var item in m.GetType().GetProperties())
        //                    {
        //                        foreach (var eitem in entity.GetType().GetProperties())
        //                        {
        //                            if (eitem.Name == item.Name)
        //                                eitem.SetValue(entity, item.GetValue(m));
        //                        }
        //                    }
        //                    listData.Add(entity);
        //                }
        //            }
        //            // CommonMethods.db.QueryWhereList<Hourprod>(x=>x.FHour==14);

        //            // listData = CommonMethods.db.QueryWhereList<BGearEntity>(x =>  x.RFID == "123");//.ToList();//.OrderBy(x => x.ChannelNo).ToList();
        //            //await CommonMethods.dbServer.GetReturnListByRFIDAsync(strRFID);

        //            //数据显示
        //            //        CommonMethods.ShowBlankingDelegate.Invoke(listData);

        //            //数据保存本地
        //            string strData = string.Empty;//数据字符串，本地保存
        //            foreach (BGearEntity m in listData)
        //            {
        //                foreach (var item in m.GetType().GetProperties())
        //                {
        //                    strData += item.GetValue(m) + ",";
        //                }
        //                strData = strData.TrimEnd(',');
        //                //BlankingDataSaveLocal(strData);
        //            }
        //        }
        //        else
        //        {
        //            LoggerHelp.WriteLog($"电芯出站数据List为空", "SysErrorLog");
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        LoggerHelp.WriteLog($"电芯出站数据保存异常：{ex}", "SysErrorLog");
        //    }
        //    return listData;
        //}
        #endregion
    }
}
#endregion
#endregion
